1
votes

In the user registration form, I have password and password2 so that user has to enter password 2x and they have to equal each other before he/she is registered. The validation rules work fine in this case. In the update user info form. I have both password and password2 set to blank so that if the user does not enter a value in the first password field or password2 field, then the system simply saves the current password which I have stored in a temporary variable in my user model. So far so good. What I want is to trigger all the same validation rules as when registering but ONLY if the user enters a value in the first password field, but leave blank and ignore otherwise. So I guess this is some kind of conditional validation.

In the user model:

$validate = array(
'password' =>  array(                           
'password_notempty'=>array(
'rule' => 'notEmpty',
 'message' => 'Required field',
 'on' => 'create')
 ,
 'password_between'=>array(
 'rule' => array('between', 5, 15),
 'message' => 'Between 5 to 15 characters',
 'on' => 'create')
 ,
'password_alphanumeric'=>array(
'rule' => 'alphaNumeric',
'message' => 'Characters and numbers only',
'on' => 'create')
),
'password2' => array(
'password2_isequal' =>array(
'rule' => array('comparePasswords','password'),
'message' => 'Must be same value as password.')
)
);

Anyone have ideas on how to accomplish this?

2

2 Answers

0
votes

this should help getting you started: http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/

you can also look at the code for examples on how to create the validation needed.

basically you always use a different field and only map it to the real field if something is actually entered. also dont forget to include a confirmation field (to retype and confirm the correctly typed password)

the article describes how you can achieve all that will a behavior in 3-4 lines of code.

0
votes
read below url :-

http://book.cakephp.org/1.3/view/1143/Data-Validation

http://book.cakephp.org/1.3/view/1152/Core-Validation-Rules

//Or Try this:-

$validate = array(
'new_password' => array(
    'between' => array(
            'rule' => array('between', 5, 20),
            'allowEmpty' => false,
            'message' => 'You password must be between 7 and 20 characters long')),
'password' => array(
    'between' => array(
            'rule' => array('between', 5, 20),
            'allowEmpty' => false,
            'message' => 'You password must be between 7 and 20 characters long')),
 'retype_password'=>array(
    'rule'=>array('equalTo','password'),
    'message'=>'Password does not match'),
'repeat_password'=>array(
    'rule'=>array('equalTo','new_password'),
    'message'=>'Password does not match'),           
);