Cakephp: how to set multiple validation in a model for different actions?

I have a user model. In it, I set the checks that are used when registering the user. It works great. But when a user edits his profile information, I do not want to check some fields, such as password, email address, etc. How is this possible. Below is the code.

<?php class User extends AppModel{ var $name = 'User'; // used when user registers var $validate = array( 'login' => array( 'minLength' => array( 'rule' => array('minLength', '6'), 'field' => 'login', 'message' => 'mimimum 6 characters long' ) ), 'password' => array( // don't want to validate in edit profile page 'minLength' => array( 'rule' => array('minLength', '6'), 'field' => 'password', 'message' => 'minimum 6 characters long' ) ), 'email' => array( array( 'rule' => 'email', 'message' => 'please enter a valid email address' ) ) ); ?> 

Above is used when I register a user. But when the user edits his profile, I can not edit / change the user password. Therefore, each time when editing a profile, he checks the correctness of password verification. I did not add the password field to the edit profile page, I do not want to check the password field. Can I have different validation rules for different actions?

Thanks.

+4
source share
3 answers

A few ways to do this:

  • Use the on option to apply rules only to create or update actions.
  • unset unwanted rules from model to validation.

     unset($this->User->validate['password']); 
  • Use custom validation methods that are smart enough to figure out whether they should be applied or not, for example. checking if $this->id or $data['id'] or not. Not recommended if you are not sure what you are doing.

  • Use the $fieldlist parameter of the save method to limit the saving and validation to only the specified fields. Fields not included in the list are not saved or checked. Highly recommended as it also protects against mold overriding.

     $this->User->save($this->data, true, array('only', 'certain', 'fields')); 
+8
source

just before that:

 var $validate = array( 'login' => array( 'minLength' => array( 'rule' => array('minLength', '6'), 'field' => 'login', 'message' => 'mimimum 6 characters long' ) ), 

for an easy way to validate jquery validation validation .

0
source

Try the following:

 <?php class User extends AppModel{ var $name = 'User'; // used when user registers var $validate = array( 'login' => array( 'minLength' => array( 'rule' => array('minLength', '6'), 'field' => 'login', 'message' => 'mimimum 6 characters long' ) ), 'password' => array( // don't want to validate in edit profile page 'minLength' => array( 'rule' => array('minLength', '6'), 'field' => 'password', 'message' => 'minimum 6 characters long', 'on' => 'create' //Only perform validation when creating a new record. ) ), 'email' => array( array( 'rule' => 'email', 'message' => 'please enter a valid email address' ) ) ); ?> 

Note the new line in the validation array for the password.

This is described in the Cookbook .

0
source

All Articles