Yii2 Add form field not to model

As known,

<?= $form->field($model, 'name_field')->textInput() ?>

      

Adds a text field connected to 'name_field' in the model / table.

I want to add a NOT field to the model / table and then run some JS when it loses focus to calculate other fields.

How do you first add a free textbox not connected to the model? Second, does anyone have any examples of adding JS / Jquery to _form.php?

+3


source to share


2 answers


The class Html

contains functions for generating fields. In fact, your code above ends up with a call Html::textInput()

. To add a field

<?= Html::textInput("name", $value) ?>

      



To add javascript to the view, just use registerJs()

:

$this->registerJs("alert('true');");

      

+3


source


You can have a field created in the same way as an ActiveField with a label and classes. For example, add the Cc field to the Mail form .

First, display the To: field (in the model):

<?= $form->field($model, 'to')->textInput() ?>

      

Lets add a Cc field (not in the model):



<?= Html::beginTag('div', ['class' => 'form-group field-mail-cc']) ?>
<?= Html::label('Cc:', 'mail-cc', ['class' => 'control-label']) ?>
<?= Html::textInput('Mail[cc]', '', ['id' => 'mail-cc', 'class' => 'form-control']) ?>
<?= Html::endTag('div') ?>

      

The class names and IDs mail-cc and field-mail-cc follow the ActiveForm naming pattern. Input name Mail [cc] adds your field to the ActiveForm group, so you can get it easily with the usual

$form = Yii::$app->request->post('Mail');

      

0


source







All Articles