Yii Framework Analysis Note 3: Form model and validation

Source: Internet
Author: User
Tags vars

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
  1. Validation rules in the model
  2. Public function rules ()
  3. {
  4. return Array (
  5. Array (' username, password ', ' required '),
  6. Array (' rememberme ', ' Boolean '),
  7. Array (' password ', ' authenticate '),
  8. );
  9. }
[PHP]View Plaincopy
  1. $model =new LoginForm; Create a login form model
  2. $model->attributes=$_post[' loginform ']; Incoming login data
  3. Cmodel->setattributes ($values,$safeOnly =true)//setter method for calling CModel
  4. Returns the security data defined in the model in Relues and obtains a validator collection through the following invocation process
  5. Each authenticator by Cvalidator::createvalidator ($rule [1], $this, $rule [0],array_slice ($rule, 2))
  6. Cmodel->getvalidators ()
  7. ->createvalidators ()
  8. ->rules ()
  9. 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
  1. Public static function Createvalidator ($name,$object,$attributes,$params =Array ())
  2. {
  3. if (is_string ($attributes))
  4. $attributes =preg_split ('/[\s,]+/',$attributes, -1,preg_split_no_empty);
  5. if (isset ($params [' on ']))
  6. {
  7. if (is_array ($params [' on ']))
  8. $on =$params [' on '];
  9. Else
  10. $on =preg_split ('/[\s,]+/',$params [' on '],-1,preg_split_no_empty);
  11. }
  12. Else
  13. $on =Array ();
  14. if (method_exists ($object,$name))
  15. {
  16. $validator =new Cinlinevalidator;
  17. $validator->attributes=$attributes;
  18. $validator->method=$name;
  19. if (isset ($params [' Clientvalidate ']))
  20. {
  21. $validator->clientvalidate=$params [' clientvalidate '];
  22. unset ($params [' clientvalidate ']);
  23. }
  24. $validator->params=$params;
  25. if (isset ($params [' Skiponerror ']))
  26. $validator->skiponerror=$params [' Skiponerror '];
  27. }
  28. Else
  29. {
  30. $params [' attributes ']=$attributes;
  31. if (Isset (self::$builtInValidators [$name]))
  32. $className =yii::import (self::$builtInValidators [$name],true);
  33. Else
  34. $className =yii::import ($name, true);
  35. $validator =new $className;
  36. foreach ($params as $name =$value)
  37. $validator$name =$value;
  38. }
  39. $validator->on=Empty ($on)?  Array (): array_combine ($on,$on);
  40. return $validator;
  41. }

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

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.