Describes the labels and styles of the input fields of the custom Yii2 form.
In Yii2, the support components for forms and fields are ActiveForm and ActiveField,
To generate a logon box in the format of label, input, and error, you can write the following code:
<?php $form = ActiveForm::begin([ 'id' => 'login-form', 'options' => ['class' => 'form-horizontal'], 'fieldConfig' => [ 'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>", 'labelOptions' => ['class' => 'col-lg-1 control-label'], ], ]); ?> <?= $form->field($model, 'username') ?> <?= $form->field($model, 'password')->passwordInput() ?>
The above code generates two form input boxes with default tag names (such as Username and Password ).
To change the label and style of the input box, you can use either of the following methods:
Copy codeThe Code is as follows:
<? = $ Form-> field ($ model, 'username', ['labeloptions' => ['label' => 'Nick name ', 'class' => 'your own class'])?>
Another more object-oriented method is:
Copy codeThe Code is as follows:
<? = $ Form-> field ($ model, 'username')-> textInput ()-> hint ('Please enter your nick name instead of Address ') -> label ('Nick name')?>