Yii framework form usage example

Source: Internet
Author: User
This article mainly introduces the form usage in the Yii framework. The example analyzes the implementation methods and related skills of form in Yii, which is very useful. For more information, see

This article mainly introduces the form usage in the Yii framework. The example analyzes the implementation methods and related skills of form in Yii, which is very useful. For more information, see

This example describes the Yii framework form usage. Share it with you for your reference. The specific method is as follows:

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:

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:

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:

The Code is as follows:

Array ('butbutelist', 'validator', 'on' => 'scenariolist',... additional options)


Parameters:

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.

Ii. Selection of values 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:

'Cikids' => array (self: has_token, 'relationships ', 'Post _ id '),

Category. php model has the following methods:

The Code is as follows:

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:

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:


<? Php echo $ form-> labelEx ($ model, 'cid');?>
<? Php echo $ form-> checkBoxList ($ model, 'cid ',
Category: getAllCategory (), array (
'Style' => 'display: inline ;',
'Paramator' =>"
N ",
'Template' => '{input} {label }',
'Labeloptions' => array ('style' => 'display: inline ')));
?>
<? Php echo $ form-> error ($ model, 'cid');?>


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)

I hope this article will help you design PHP programs based on the Yii framework.

Related Article

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.