PHP Development Framework YII Framework Tutorial (8) using Formmodel

Source: Internet
Author: User
Tags html form yii

Through the previous study, we learned about the basic components of YII Web applications, but also write simple applications such as hangman guessing word games. In the first example Yii Framework development Concise Tutorial (1) The first application Hello world we introduced the MVC Web application using the MVC model, and also explained that the purpose of this tutorial is to use different perspectives (primarily through the development of Windows applications c++,c# Programmer's perspective) helps Windows desktop applications or ASP.net programmers to quickly master the PHP YII Framework application Framework.

Earlier we described creating view (Page view Form) through cHTML, Ccontroller to handle user-submitted events, and an analogy to Windows desktop applications or asp.net, Yii views (HTML form) A page similar to WinForm or asp.net. Control class controller event handling (Code-behind) classes similar to Windows desktop applications or asp.net. The difference is that asp.net and Windows desktop applications can define IDs for individual UI components in the UI, such as text boxes, buttons, and then add event handling for different UI components. There is no mechanism for PHP applications or YII applications to define an ID for the UI component defined in the HTML form and to define event handling for the UI component. However, the YII framework provides cformmodel to support similar functionality, simply by Cformmodel, you can define variables for UI groups in HTML Form, and you can access them in their control class controller. Each yii View (Form) generally provides a "submit" button (submit button) that the user clicks on to trigger the Ccontroller object's corresponding actionxxx method in Actionxxx method to access the value of the UI component of the HTML form through Cformmodel.

In the previous tutorial, the model in Yii is an instance of Cmodel or its subclasses. The model is used to maintain data and its associated business logic,

YII implements two types of models: the form model and the Active record. Both inherit from the same base class Cmodel.

The form model is an instance of Cformmodel. The form model is used to keep the data obtained from the user's input. These data are often fetched, used, and discarded. For example, in a login page, we can use the form model to represent the user name and password information provided by the end user. For more information, please refer to using the form. This article describes the use of Cformmodel,

Active Record (AR) is a design pattern for abstracting database access through an object-oriented style. Each AR object is an instance of a Cactiverecord or its subclasses. Represents a row in a datasheet. The fields in the row correspond to the properties in the AR object. For more details on AR Please read the Active record. The following is a brief introduction to database usage.

This article uses a simple login interface to introduce the use of Formmodel, this example to download.

1. Define Model Classes

Below we created a loginform (protected/models/loginform.php) model class to collect user input in a login page. Because the login information is only used to authenticate the user, it does not need to be saved, so we create the loginform as a form model.

Class LoginForm extends 

cformmodel     
{public     
    $username;     
    public $password;     
    Public $rememberMe =false;     
}

2. Declaring validation rules

Once the user submits his input and the model is populated, we need to ensure that the user's input is valid before use. This is done by validating the user's input and a series of rules. We specify these validation rules in the Rules () method, and this method should return a rule configuration array.

Class LoginForm extends Cformmodel     
{public     
    $username;     
    public $password;     
    Public $rememberMe =false;     
         
    Private $_identity;     
         
    Public function rules ()     
    {return     
        array (     
            ' username, password ', ' required '),     
            Array (' RememberMe ', ' Boolean '),     
            Array (' Password ', ' Authenticate '),     
        );     
    }     
         
    The Public function authenticate ($attribute, $params)     
    {     
        $this->_identity=new useridentity ($this-> Username,     
              $this->password);     
        if (! $this->_identity->authenticate ())     
            $this->adderror (' Password ', ' incorrect username or password. ');     
    }     
}

The above code specifies that: username and password are required, password should be validated (authenticated), and RememberMe should be a Boolean value.

Each rule returned by rules () must be in the following format:

Array (' AttributeList ', ' Validator ', ' on 

' => ' scenariolist ', ...) Additional options)

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.