The previous introduction to the YII Framework Development tutorial (7) using cHTML to create form and YII Framework Development Tutorial (8) Using Formmodel has been a brief introduction to Cactiveform. It's Christmas in a few days, and it helps Santa make a survey of what everyone wants for Christmas and the food:-) they want to eat for Christmas dinner.
Here we share some of the code as a framework for subsequent examples, basically copying Hello World's example, adding an empty Datamodel, Using the default controller (Sitecontroller) and default action (Actionindex), use the default configuration file for code downloads.
Collecting user data through HTML forms is one of the main tasks of web application development. In addition to the form design, developers 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 through its MVC structure.
When working with forms in Yii, you typically need the following steps:
Create a model class to represent the data fields that you want to collect.
Create a controller action that responds to the form submission.
Create a form associated with 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 (' FirstName, lastName ', ' required '),
Array (' Favouritegift,favouritedinner ', ' safe '));
Static $gifts =array (' 1 ' => ' IPad ', ' 2 ' => ' Remote Control helicopter '),
' 3 ' => ' 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 is used to record user names, $favouriteGift and $favouritedinner to record the user's gift and food name. $gifts, $meals statically defines the types of gifts and types of food that users can choose. Note here that the firstname,lastname is required, and $favouriteGift and $favouritedinner are set to be safely replicated, as defined by the rules of the model.
Define Action
To modify the Sitecontroller action method:
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 view,index are defined here to get user input, choice display user selection results. $model->attributes=$_post [' Datamodel ']; As we said in the security attribute assignment, this line of code populates the model with data submitted by the user. The Attributes property is defined by the Cmodel, which accepts an array of name values and assigns each value to the corresponding model attribute.
Define View
The first is the definition of index.php, which uses cactiveform,cactiveform to provide seamless, consistent validation both at the client and server side.
<?php $form = $this-
>beginwidget (' Cactiveform ', array (
' id ' => ' user-form ',
' Enableajaxvalidation ' =>true,
' enableclientvalidation ' =>true,
' Focus ' =>array ($model, ' FirstName '))
;?>