Yii Framework form model and validation usage _php instance

Source: Internet
Author: User
Tags php programming smarty template yii

This example describes the YII framework form model and validation usage. Share to everyone for your reference, specific as follows:

The form model Cformmodel most of the inherited Cmodelcmodel, because the table model data does not need to be persisted, so it is primarily on the validation operation. The following is an example of a form model that takes a Web site login generated by a frame scaffold.

Validation rules in the model public
function rules (.)
{return Array (
    ' username, password ', ' required '),
    Array (' RememberMe ', ' Boolean '),
    Array (' Password ', ' Authenticate '),
  );
}

$model =new loginform;//Create a login form model
$model->attributes=$_post[' loginform '];//incoming logon data
cmodel-> SetAttributes ($values, $safeOnly =true)//Call Cmodel Setter method
//Return the security data defined in model Relues, get a validator collection through the following call process
//Validator by Cvalidator::createvalidator ($rule [1], $this, $rule [0],array_slice ($rule, 2))
Cmodel->getvalidators ()->createvalidators ()->rules () cmodel->validate ()
;// Traverse Validator to perform validation

Validator Internal implementation

The core parts of the form model are validated, and the implementation is discussed below.

The validation in the YII framework exists as a collection of independent components, and Cvalidator is the base class for all validators. Or, for example, login validation. The Rules method contains the required, Boolean, authenticate three validators

public static function Createvalidator ($name, $object, $attributes, $params =array ()) {if (is_string ($attributes)) $att
  Ributes=preg_split ('/[\s,]+/', $attributes, -1,preg_split_no_empty);
    if (Isset ($params [' on '])) {if (Is_array ($params [' in ']) $on = $params [' on '];
  else $on =preg_split ('/[\s,]+/', $params [' on '],-1,preg_split_no_empty);
  else $on =array ();
    if (Method_exists ($object, $name)) {$validator =new cinlinevalidator;
    $validator->attributes= $attributes;
    $validator->method= $name;
      if (Isset ($params [' clientvalidate ']) {$validator->clientvalidate= $params [' clientvalidate '];
    unset ($params [' clientvalidate ']);
    } $validator->params= $params;
  if (Isset ($params [' Skiponerror ']) $validator->skiponerror= $params [' Skiponerror '];
    else {$params [' attributes ']= $attributes;
    if (Isset (self:: $builtInValidators [$name]) $className =yii::import (self:: $builtInValidators [$name],true); Else
      $className =yii::import ($name, true);
    $validator =new $className;
  foreach ($params as $name => $value) $validator-> $name = $value; $validator->on=empty ($on)?
  Array (): Array_combine ($on, $on);
return $validator;

 }

Create Validator Process

1. Get Properties (array $attributes) and use Scene (array $on)
2, to determine whether the validator exists in model, if there is a create an inline validator cinlinevalidator, if it does not exist, perform step 3rd
3, if the validator is a frame with the import of its own validator, otherwise import the external validator, and then instantiate and assign a value.

When the validate () is triggered, the possible errors are stored in model, which can be extracted by calling Cmodel::geterrors () and Cmodel::geterror ()

PS: Small knitting here recommend a site for the layout of the PHP format landscaping tools to help you in the future of PHP programming code layout:

PHP Code online Format Landscaping tool:Http://tools.jb51.net/code/phpformat

More about Yii related content readers can view the site topics: "Yii framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Tutorial", "PHP date and Time usage summary", "PHP object-oriented Programming Program", " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

I hope this article will help you with the PHP program design based on the YII framework.

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.