Learning notes using form in Yii Framework

Source: Internet
Author: User

Use form

To process a form in Yii, follow these steps:
1. Create a model class to represent the fields to be collected.
2. Create a controller action and submit the response form.
3. Create a form related to the controller action in the view script.
I. Create a model
Before writing the HTML code required for the form, we should first determine the type of data input from the end user and the rules that the data should comply. The model class can be used to record this information. As defined in the model section, the model is the central location for storing user input and verifying these inputs.
You can create two types of models based on the data input by the user. If user input is collected, used, and discarded, we should create a form model. If user input is collected and saved to the database, we should use an Active Record. The two types of models share the same basic class CModel, which defines the common interfaces required by the form.
1. Define model classes
For example, create a form model:

The code is as follows: Copy code
Class LoginForm extends CFormModel
{
Public $ username;
Public $ password;
Public $ rememberMe = false;
}

LoginForm defines three attributes: $ username, $ password, and $ rememberMe. They are used to save the user name and password entered by the user, and whether the user wants to remember his logon options. Because $ rememberMe has a default value of false, the corresponding options are not checked during initialization.
These member variables are called attributes instead of properties to distinguish them from common properties ). Attribute is an attribute used to store user input or database data ).
2. Declare verification rules
Once the user submits his input and the model is filled, we need to ensure that the user input is valid before use. This is achieved by verifying user input and a series of rules. We specify these verification rules in the rules () method. This method should return an array of rule configurations.

The code is as follows: Copy code

Class LoginForm extends CFormModel
{
Public $ username;
Public $ password;
Public $ rememberMe = false;

Private $ _ identity;

Public function rules ()
{
Return array (
Array ('username, password', 'required'), // username and password are required
Array ('rememberme', 'boolean'), // rememberMe should be a boolean value
Array ('password', 'authenticate'), // password should be verified (authenticated)
);
}

Public function authenticate ($ attribute, $ params)
{
$ This-> _ identity = new UserIdentity ($ this-> username, $ this-> password );
If (! $ This-> _ identity-> authenticate ())
$ This-> addError ('password', 'indicates the incorrect user name or password. ');
}
}

Each rule returned by rules () must be in the following format:
Array ('butbutelist', 'validator', 'on' => 'scenariolist',... additional options)
Where:
AttributeList (feature list) is a feature list string that needs to be verified by this rule. Each feature name is separated by a comma;
Validator (validators) specifies the type of verification to be performed;
The on parameter is optional. It specifies the list of scenarios that the rule should be applied;
The additional option is an array of name-value pairs used to initialize the attribute values of the corresponding validators.


Select value when updating data in form


The category table and the post table are many-to-many. There is an intermediate table relationships, respectively remembering category_id and post_id.

Post. php model

The code is as follows: Copy code

'Cikids' => array (self: has_token, 'Relationships ', 'post _ id '),


Category. php model has the following methods:

The code is as follows: Copy code

Static public function getAllCategory (){
Return CHtml: listData (self: model ()-> findAll (), 'id', 'name ');
 }

For example, if I want to update a piece of data, there are two columns of this data. If the id of this article is 21 and it belongs to two columns, the data in the relationship table should be

The code is as follows: Copy code
Id post_id category_id
1 21 1
2 21 2

Here, id is the stream, and the category of this article is 1 and 2. The data of this topic can be obtained by establishing the AR of Relationship. php,

In _ from, the form is written as follows:

The code is as follows: Copy code

<Div class = 'row'>
<? Php echo $ form-> labelEx ($ model, 'CID');?>
<? Php echo $ form-> checkBoxList ($ model, 'CID ',
Category: getAllCategory (), array (
'Style' => 'Display: inline ;',
'Paramator' => "<br/> n ",
'Template' => '{input} {label }',
'Labeloptions' => array ('style' => 'Display: inline ')));
?>
<? Php echo $ form-> error ($ model, 'CID');?>
</Div>

The problem is that I don't know if I want to compress the data in _ form? That is, when I update data, the topic should be selected.

For the data decoupling at The view layer, aside from the checkBoxList, dropDownList is used as an example:

1 => Category 1, 2 => Category 2, which may be ''=> Select, 1 => Category 1, 2 => Category 2. So what do you think?

Behavior is like this. Behavior is just a solution. Let's talk about it later. What you need to understand at present is, if you want to provide an attribute (like cid []) for the Model, what time do you need to consider? (Tip: to be grounded with CActiveRecord)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.