Yii Framework form model and verification usage, yii Framework form model. Yii Framework form model and verification usage. This document describes the yii Framework form model and verification usage. For your reference, we will share the following details: Form model Yii Framework form model and verification usage, yii Framework form model
This document describes the Yii Framework form model and verification usage. We will share this with you for your reference. The details are as follows:
Most form model cformmodels inherit CModelCModel. Because table model data does not need to be persisted, it is mainly used for verification. The following describes the form model by taking website login generated by frame scaffolding as an example.
// Verification rules in the model public function rules () {return array ('username, password', 'required'), array ('memberme', 'boolean '), array ('password', 'authenticate '),);}
$ Model = new LoginForm; // create a login form model $ model-> attributes = $ _ POST ['loginform']; // input the login data CModel-> setAttributes ($ values, $ safeOnly = true) // call the setter method of CModel // return the security data defined in relues in the model, obtain a set of validators through the following call process // Each validators is composed of CValidator: createValidator ($ rule [1], $ this, $ rule [0], array_slice ($ rule, 2) CModel-> getValidators ()-> createValidators ()-> rules () CModel-> validate (); // The traversal validator performs verification.
Internal Implementation of validators
The core part of the form model is verification. the implementation method is discussed below.
In the YII Framework, verification exists in the form of an independent component set. CValidator is the base class of all validators. Take logon verification as an example. The rules method contains three validators: required, boolean, and authenticate.
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;}
Process for creating validators
1. get attributes (array $ attributes) and use cases (array $ on)
2. check whether the validators exist in the model. If an inline validators CInlineValidator exists, if not, perform step 1.
3. if the validators are built-in framework import built-in validators, otherwise, import the external validators and then instantiate and assign values.
When validate () is triggered, possible errors are stored in the model. you can call CModel: getErrors () and CModel: getError () to extract these error messages.
PS: I recommend a php formatting and formatting typographical tool on this site to help you typeset code in future PHP programming:
Php code online formatting and beautification tools:Http://tools.jb51.net/code/phpformat