Yii2添加不在模型中的表单字段

时间:2022-11-24 20:15:55

As we know,

据我们所知,

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

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

在模型/表中添加连接到“name_field”的文本字段。

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

我想在模型/表中添加一个字段NOT,然后当它失去焦点来计算其他字段时运行一些JS。

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

首先如何添加未连接到模型的*文本字段?第二,有没有人有任何向_form.php添加JS / Jquery的例子?

2 个解决方案

#1


The Html class contains the functions for generation of fields. In fact, your code above ends up calling Html::textInput(). To add a field

Html类包含用于生成字段的函数。实际上,上面的代码最终调用了Html :: textInput()。添加字段

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

To add javascript to a view just use registerJs():

要将javascript添加到视图中,只需使用registerJs():

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

#2


You can have the field rendered the same way as the ActiveField, with a label and classes. For example, let’s add a Cc field to a Mail form.

您可以使用与标签和类相同的方式将字段呈现为与ActiveField相同的方式。例如,让我们将一个抄送字段添加到邮件表单。

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

首先显示To:字段(在模型中):

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

Let’s add the 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 and id names mail-cc and field-mail-cc follow the ActiveForm naming pattern. The input name Mail[cc] adds your field to the ActiveForm group, so you can easily retrieve it with the usual

类和id名称mail-cc和field-mail-cc遵循ActiveForm命名模式。输入名称Mail [cc]将您的字段添加到ActiveForm组,因此您可以使用常规方法轻松检索它

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

#1


The Html class contains the functions for generation of fields. In fact, your code above ends up calling Html::textInput(). To add a field

Html类包含用于生成字段的函数。实际上,上面的代码最终调用了Html :: textInput()。添加字段

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

To add javascript to a view just use registerJs():

要将javascript添加到视图中,只需使用registerJs():

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

#2


You can have the field rendered the same way as the ActiveField, with a label and classes. For example, let’s add a Cc field to a Mail form.

您可以使用与标签和类相同的方式将字段呈现为与ActiveField相同的方式。例如,让我们将一个抄送字段添加到邮件表单。

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

首先显示To:字段(在模型中):

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

Let’s add the 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 and id names mail-cc and field-mail-cc follow the ActiveForm naming pattern. The input name Mail[cc] adds your field to the ActiveForm group, so you can easily retrieve it with the usual

类和id名称mail-cc和field-mail-cc遵循ActiveForm命名模式。输入名称Mail [cc]将您的字段添加到ActiveForm组,因此您可以使用常规方法轻松检索它

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