yii2 ActiveForm numeric textfield

Ofershap picture Ofershap · Nov 9, 2015 · Viewed 31.5k times · Source

I've created an ActiveForm using yii2 like this:

                            <?=$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)?>

and it rendered a result of:

<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>

now i want to make it < input type="number" .. and not text.. (so user could change value using browser up/down buttons). is there any way to do it?

Answer

Touqeer Shafi picture Touqeer Shafi · Nov 9, 2015

You can use ->textInput(['type' => 'number'] eg :

<?=$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)?>