Home > Article > PHP Framework > How to use forms in yii framework
How to create a form page that allows users to submit data. The page will display a form containing a name input box and an email input box. After submitting these two parts of information, the page will display the information entered by the user. (Recommended learning: yii framework)
In order to achieve this goal, in addition to creating an operation and two views, you also need to create a model.
Through this tutorial, you will learn:
Create a model to represent the data entered by the user through the form
Declare rules to validate the input Data
Generate an HTML form in the view
Create model
The model class EntryForm represents the data requested from the user. The class is as follows and stored in models/EntryForm.php file.
<?php namespace app\models; use Yii; use yii\base\Model; class EntryForm extends Model { public $name; public $email; public function rules() { return [ [['name', 'email'], 'required'], ['email', 'email'], ]; } }
This class inherits from a base class yii\base\Model provided by Yii, which is usually used to represent data.
信息: yii\base\Model 被用于普通模型类的父类并与数据表无关。yii\db\ActiveRecord 通常是普通模型类的父类但与数据表有关联(译注:yii\db\ActiveRecord 类其实也是继承自 yii\base\Model,增加了数据库处理)。
The EntryForm class contains two public members: name and email, which are used to store user-entered data. It also contains a method called rules() that returns a collection of data validation rules. The validation rules declared above indicate:
name and email values are both required
The value of email must satisfy the email rule verification
If you have a system that processes user-submitted data EntryForm object, you can call its validate() method to trigger data validation. If there is data validation failure, the hasErrors attribute will be set to true. If you want to know what error occurred, call getErrors.
<?php $model = new EntryForm(); $model->name = 'Qiang'; $model->email = 'bad'; if ($model->validate()) { // 验证成功! } else { // 失败! // 使用 $model->getErrors() 获取错误详情 }
Create action
Next you have to create an entry operation in the site controller for the new model.
<?php namespace app\controllers; use Yii; use yii\web\Controller; use app\models\EntryForm; class SiteController extends Controller { // ...现存的代码... public function actionEntry() { $model = new EntryForm; if ($model->load(Yii::$app->request->post()) && $model->validate()) { // 验证 $model 收到的数据 // 做些有意义的事 ... return $this->render('entry-confirm', ['model' => $model]); } else { // 无论是初始化显示还是数据验证错误 return $this->render('entry', ['model' => $model]); } } }
This operation first creates an EntryForm object. Then try to collect user-submitted data from $_POST, which is collected by Yii's yii\web\Request::post() method. If the model is successfully populated with data (that is, the user has submitted the HTML form), the operation will call validate() to ensure that the user submitted valid data.
信息: 表达式 Yii::$app 代表应用实例,它是一个全局可访问的单例。 同时它也是一个服务定位器, 能提供 request,response,db 等等特定功能的组件。 在上面的代码里就是使用 request 组件来访问应用实例收到的 $_POST 数据。
After the user submits the form, the operation will render a view named entry-confirm to confirm the data entered by the user. If the form is submitted without filling out the form, or if the data contains errors (Translator: For example, the email format is incorrect), the entry view will render the output, along with the form and the details of the validation error.
Note: In this simple example we only present the confirmation page with valid data. In practice you should consider using refresh() or redirect() to avoid the problem of repeated form submission.
Create a view?
Finally create two view files entry-confirm and entry. They will be rendered by the entry operation just created.
The entry-confirm view simply displays the submitted name and email data. The view file should be saved in views/site/entry-confirm.php.
<?php use yii\helpers\Html; ?> <p>You have entered the following information:</p> <ul> <li><label>Name</label>: <?= Html::encode($model->name) ?></li> <li><label>Email</label>: <?= Html::encode($model->email) ?></li> </ul>
The entry view displays an HTML form. View files should be saved in views/site/entry.php.
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; ?> <?php $form = ActiveForm::begin(); ?> <?= $form->field($model, 'name') ?> <?= $form->field($model, 'email') ?> <div class="form-group"> <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?>
Use your browser to access the following URL to see if it works:
http://hostname/index.php?r=site/entry
You will see a page containing a form with two input boxes. Each input box has a label in front of it indicating the type of data that should be entered. If you click the submit button without filling in anything, or fill in an email address with an incorrect format, you will see an error message displayed under the corresponding input box.
After entering valid name and email information and submitting, you will see a confirmation page showing the data you submitted.
The above is the detailed content of How to use forms in yii framework. For more information, please follow other related articles on the PHP Chinese website!