yii2 ActiveForm数字文本字段

Ofe*_*hap 9 yii2 active-form yii2-advanced-app

我使用yii2创建了一个ActiveForm,如下所示:

                            <?=$form->field($item, 'finalPrice', [
                                'options' => [
                                    'tag' => 'div',
                                    'class' => '',
                                ],
                                'template' => '<span class="col-md-2 col-lg-2"><label class="control-label">Final item price</label>{input}{error}</span>'
                            ])->textInput([
                                 // ** i want numeric value **
                            ])->label(false)?>
Run Code Online (Sandbox Code Playgroud)

它产生了以下结果:

<span class="col-md-2 col-lg-2"><label class="control-label">Final item price</label><input type="text" id="item-finalprice" class="form-control" name="Item[finalPrice]"><p class="help-block help-block-error"></p></span>
Run Code Online (Sandbox Code Playgroud)

现在我想让它<input type ="number"..而不是文本..(因此用户可以使用浏览器向上/向下按钮更改值).有什么办法吗?

Tou*_*afi 30

您可以使用->textInput(['type' => 'number']例如:

<?=$form->field($item, 'finalPrice', [
                                'options' => [
                                    'tag' => 'div',
                                    'class' => '',
                                ],
                                'template' => '<span class="col-md-2 col-lg-2"><label class="control-label">Final item price</label>{input}{error}</span>'
                            ])->textInput([
                                 'type' => 'number'
                            ])->label(false)?>
Run Code Online (Sandbox Code Playgroud)


Shu*_*man 7

尝试这个 。这对我有用

<?= $form->field($model, 'amount')->textInput(['type' => 'number']) ?>
Run Code Online (Sandbox Code Playgroud)

  • 也可以很好地指定“min”、“max”和“step”值。`&lt;?= $form-&gt;field($model, 'position')-&gt;input('number', ['min' =&gt; 0, 'max' =&gt; 10000, 'step' =&gt; 1]) ?&gt; ` (2认同)