Most of the form model Cformmodel inherit Cmodelcmodel, because the table model data does not need to be persisted, so it is mainly on the authentication operation. The following is an example of a Web site login with framework scaffolding to illustrate the form model.
[PHP]View Plaincopy
- Validation rules in the model
- Public function rules ()
- {
- return Array (
- Array (' username, password ', ' required '),
- Array (' rememberme ', ' Boolean '),
- Array (' password ', ' authenticate '),
- );
- }
[PHP]View Plaincopy
- $model =new LoginForm; Create a login form model
- $model->attributes=$_post[' loginform ']; Incoming login data
- Cmodel->setattributes ($values,$safeOnly =true)//setter method for calling CModel
- Returns the security data defined in the model in Relues and obtains a validator collection through the following invocation process
- Each authenticator by Cvalidator::createvalidator ($rule [1], $this, $rule [0],array_slice ($rule, 2))
- Cmodel->getvalidators ()
- ->createvalidators ()
- ->rules ()
- Cmodel->validate (); //Traversal validator performs validation
Validator Internal implementation
The core part of the form model is on validation, and the following is a discussion of how it is implemented.
The validation in the YII framework exists as a collection of independent components, and Cvalidator is the base class for all validators. Also, take login verification as an example. The Rules method contains required, Boolean, authenticate three validators
[PHP]View Plaincopy
- Public static function Createvalidator ($name,$object,$attributes,$params =Array ())
- {
- if (is_string ($attributes))
- $attributes =preg_split ('/[\s,]+/',$attributes, -1,preg_split_no_empty);
- if (isset ($params [' on ']))
- {
- if (is_array ($params [' on ']))
- $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 attributes (array $attributes) and usage scenarios (array $on)
2, to determine whether the validator exists in the model, if there is an inline validator created cinlinevalidator, if not present, perform the 3rd step
3. If the authenticator is an imported self-brought authenticator with the framework, import the external validator, and then instantiate and assign the value.
When validate () is triggered, errors that may be generated will be stored in the model, and these error messages can be extracted by calling Cmodel::geterrors () and Cmodel::geterror ().
Yii Framework Analysis Note 3: Form model and validation