Expanded Custom Verification rules in cakephp

Source: Internet
Author: User
Cakephp extends custom verification rules & nbsp; in CAKEPHP, there are up to 26 verification rules can be used, see here for details: http://book.cakephp.org/view/1181/Adding-your-own-Validation-Methods! /View/1152/Core-Validati cakephp extends custom verification rules
In CAKEPHP, up to 26 verification rules can be used. for details, see here:
Http://book.cakephp.org/view/1181/Adding-your-own-Validation-Methods! /View/1152/Core-Validation-Rules

But how to extend the customization? For example, in an example, only the administrator user needs to check and enter the user name.
(An extreme example), you can do this:
Class User extends AppModel {
Var $ name = 'user ';
Var $ validate = array (
'Name' => array ('notempty '=>
Array ('rule' => array ('notempty '),
'Message' => 'Please enter first name ',
......

.......
'Username' => array ('custom' =>
Array ('rule' => array ('validatedependentfields '),
'Message' => 'Please enter height ',
Here, we use a custom method for username to define a method validateDependentFields for verification.

Method:
function validateDependentFields($field){     $passed=true;     switch(true){         case array_key_exists('username',$field):             if(  $this->data['User']['user_type_id']==1 and (!isset($this->data['User']['username']) or empty($this->data['User']['username'])) ){                 $passed=false;             }else{                 $passed=true;             }         break;         case array_key_exists('class_of',$field):             if(  $this->data['User']['user_type_id']==1 and (!isset($this->data['User']['class_of']) or empty($this->data['User']['class_of'])) ){                 $passed=false;             }else{                 $passed=true;             }         break;     }     return $passed; }


If user_type_id = 1 is the administrator, true is returned, indicating that verification rules are used. otherwise, false is returned.

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.