The previous section introduced YiiFramework development tutorial (7) using CHtml to create Form and YiiFramework 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 ). 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 ).
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
classDataModel 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 and lastName are used to record the user name, $ favouriteGift and $ favouriteDinner to 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,'enableClientValidation'=>true,'focus'=>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 GiftradioButtonList ($ model, 'favouritegift ', DataModel: $ gifts)?> Choose your Christmas dinnercheckBoxList ($ model, 'favouritedinner ', DataModel: $ meals)?> EndWidget ();?> View Choice is simpler. the user-selected result is displayed: 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 result is as follows:
vcyzzCgxMSkgVUnX6bz+IEFjdGl2ZUZvcm3KvsD9" src="/uploadfile/2016/0106/20160106062558152.png" />
The above is the sample content of the UI component ActiveForm in the PHP development Framework Yii Framework tutorial (11). For more information, see PHP Chinese website (www.php1.cn )!