Home >PHP Framework >YII >How to write a yii framework form?

How to write a yii framework form?

angryTom
angryTomOriginal
2020-01-21 18:31:281742browse

In Yii, forms (form widgets) are mainly created through the yii\widgets\ActiveForm class. Let's introduce how to write forms in yii. We hope it will be helpful to students who are learning the yii framework!

How to write a yii framework form?

How to write a yii framework form?

(1) First create a form instance

<?php $form=ActiveForm::begin();?> //$form是表单实例
...
<?php ActiveForm::end();?>

ActiveForm::begin() not only marks the start of the form, but also creates a form instance.

Recommended learning: yii tutorial

(2) Then create an ActiveField instance

  <?php $form=ActiveForm::begin();?>
+  <?= $form->field($model,&#39;title&#39;)?>
  <?php ActiveForm::end();?>

and place it in ActiveForm: Everything between :begin() and ActiveForm::end() is wrapped in the HTML ff9c23ada1bcecdd1a0fb5d5a0f18437 tag.

Create an ActiveField instance by calling the ActiveForm::field() method. This instance will create the form element and the element's label, as well as the corresponding JavaScript response.

Each ActiveField has a corresponding model and attribute. The name attribute of the input input box will be automatically created based on the attribute name; at the same time, the attribute verification rules will be used to verify the user's data input.

(3) Create an ActiveField object using the example

<?= $form->field($model,&#39;body&#39;)->textarea([&#39;rows&#39;=>3]) =?>
  • $form->field($model,'body');

  • ActiveField's method textarea() creates a 3-line input box;

The above is the detailed content of How to write a yii framework form?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn