Home  >  Article  >  Backend Development  >  PHP development framework Yii Framework tutorial (11) UI component ActiveForm example

PHP development framework Yii Framework tutorial (11) UI component ActiveForm example

黄舟
黄舟Original
2017-01-21 10:08:461089browse

In the previous introduction to the Yii Framework development tutorial (7) using CHtml to create a Form and the Yii Framework development tutorial (8) using FormModel, CActiveForm has been briefly introduced. Christmas will be here in a few days. Here, help Santa Claus make a survey about the Christmas gifts everyone hopes to get and the food they want to eat for Christmas dinner).

Here we share part of the code as a framework for subsequent examples. It basically copies the Hello World example, adds an empty DataModel, and uses the default Controller (SiteController) and the default Action (actionIndex). , use the default configuration file and code download.

Collecting user data through HTML forms is one of the main tasks of Web program development. In addition to form design, developers also need to populate the form with existing or default data, validate user input, display appropriate error messages for invalid input, and save input to persistent storage. Yii greatly simplifies this workflow with its MVC structure.

When processing forms in Yii, the following steps are usually required:

Create a model class that represents the data fields to be collected.

Create a controller action to respond to form submission.

Create forms related to controller actions in view scripts.

Create DataModel

class
DataModel extends CFormModel{public $firstName;public $lastName;
public $favouriteGift;
public $favouriteDinner;
public function rules(){return array(array('firstName, lastName', 'required'),
array('favouriteGift,favouriteDinner', 'safe'));}
static $gifts=array('1'=>'iPad','2'=>'Remote control helicopter','3'=>'60 inch 3D LED TV','4'=>'Holy Bible',);
static $meals=array('1'=>'Egg','2'=>'Ham','3'=>'Chicken','4'=>'Pork','5'=>'Beer','6'=>'Coke','7'=>'Wine',);}

firstName, lastName are used to record the user's name, $favouriteGift and $favouriteDinner are used to record the user's gift and food names. $gifts, $meals statically define the types of gifts and food types available to users. What should be noted here is that firstName and lastName are required, and $favouriteGift and $favouriteDinner are set to be safe to copy. These are defined by the rules of the Model.

Define Action

Modify the Action method of SiteController:

public function actionIndex(){$model=new DataModel();
if(!emptyempty($_POST['DataModel'])){$model->attributes=$_POST['DataModel'];
if($model->validate()){$this->render('choice', array('model' => $model,));
return;}}
$this->render('index', array('model' => $model,));}

Two Views are defined here, index is used to obtain user input, and choice displays the user selection results. $model->attributes=$_POST ['DataModel']; As we discussed in Safe Attribute Assignment, this line of code populates the model with user-submitted data. The attributes attribute is defined by CModel, which accepts an array of name-value pairs and assigns each value to the corresponding model attribute.

Define View

First define index.php. In this example, CActiveForm is used. CActiveForm provides seamless and consistent verification on both the client and server sides.

>beginWidget('CActiveForm', array(
'id'=>'user-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'focus'=>array($model,'firstName'),
)); ?>

This example is relatively simple. These complex checks are not used. It only requires that FirstName and LastName are not empty. This is defined by required in the rules in DataModel.

beginWidget('CActiveForm'); ?>
 
errorSummary($model); ?>
 
label($model,'firstName'); ?>textField($model,'firstName') ?>
 
 
label($model,'lastName'); ?>textField($model,'lastName') ?>
 
 
Choose your Christmas Gift
radioButtonList($model,'favouriteGift',DataModel::$gifts) ?>
 
 
 
 
Choose your Christmas dinner
checkBoxList($model,'favouriteDinner',DataModel::$meals) ?>
 
 
 
 
 
endWidget(); ?>
View Choice 就更简单,显示用户选择结果:
 
Christmas "; ?>firstName . ' ' . $model->lastName . '.' ;?>
 
You will be givenecho DataModel::$gifts[$model->favouriteGift];?>on Christmas Day.
 
 
And you will haveforeach($model->favouriteDinner as $dinner){echo DataModel::$meals[$dinner] . ' ';}?>
for Christmas dinner.


##The running results are as follows:

vcyzzCgxMSkgVUnX6bz+IEFjdGl2ZUZvcm3KvsD9" src="/uploadfile/2016/0106/20160106062558152.png" />

The above is the PHP development framework Yii Framework Tutorial (11) The content of the UI component ActiveForm example. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


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