PHP development framework YiiFramework tutorial (11) UI component ActiveForm example
The previous section introduced Yii Framework Development Tutorial (7) using CHtml to create Form and Yii Framework Development Tutorial (8) using FormModel has briefly introduced CActiveForm. It will be Christmas in a few days. here we will help Santa Claus make a survey to investigate the Christmas gifts and foods that everyone wants to eat for Christmas meals :-).
Here we share part of the code. as a framework for subsequent examples, we basically copy the Hello World example, add an empty DataModel, and use the default Controller (SiteController) and the default Action (actionIndex). use the default configuration file to download the code.
Collecting user data through HTML forms is one of the main tasks of Web application development. In addition to form design, developers also need to fill in existing or default data to form, verify user input, display appropriate error information for invalid input, and save the input to persistent storage. Yii greatly simplifies this workflow through its MVC structure.
To process a form in Yii, follow these steps:
Create a model class that represents the data fields to be collected.
Create a controller action and submit the response form.
Create a form related to the controller action in the view script.
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 le le ',
);
Static $ meals = array (
'1' => 'egg ',
'2' => 'ham ',
'3' => 'chicken ',
'4' => 'pork ',
'5' => 'beer ',
'6' => 'coke ',
'7' => 'wine ',
);
} FirstName and lastName are used to record the user name. $ favouriteGift and $ favouriteDinner record the user's gift and food name. $ Gifts, $ meals statically defines the types of gifts and foods that you can choose from. Note that firstName and lastName are mandatory, while $ favouriteGift and $ favouriteDinner are set to secure replication, which are defined by Model rules.
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. The index is used to obtain user input. choice displays the user selection result. $ Model-> attributes = $ _ POST ['datamodel ']; as we mentioned in the security feature assignment, this line of code uses the data submitted by the user to fill the model. The attributes attribute is defined by CModel. it accepts a name-value pair array and assigns each value to the corresponding model feature.
Define View
First, define index. php. In this example, CActiveForm is used. CActiveForm also provides seamless and consistent verification between the client and the server.
> BeginWidget ('cactiveform', array (
'Id' => 'User-form ',
'Enableajaxvalidation '=> true,
'Enablesclientvalidation '=> true,
'Focal '=> array ($ model, 'firstname '),
);?>
This example is relatively simple. without these complex verifications, only FirstName and LastName are required. this is defined by the required of 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 is simpler and displays the user selection result:
Christmas ";?>
FirstName. ''. $ model-> lastName. '.';?>
You will be given
Echo DataModel: $ gifts [$ model-> favouriteGift];
?>
On Christmas Day.
And you will have
Foreach ($ model-> favouriteDinner as $ dinner)
{
Echo DataModel: $ meals [$ dinner]. '';
}
?>
For Christmas dinner.
The running result is as follows: