The problem is :
1. The password field exactly only accept more than 6 character so if a user fill just 5 character it will not be valid.
2. Commonly a confirmation password has to be match with a password previously.
We can solve this problem with a model callback beforeSave, here is the code:
In the first step we cannot declare an input text field with ‘User.password’ in the fieldname but you have to choose another don’t choose passwd and password like above once again choose another may be pwd or I don’t know it’s up to you guys. So our view code will look like this:
input(‘User.pwd’,array(‘label’=>__(‘Password’,true),’type’=>’password’)); ?>See we use User.pwd rather than User.password that is usualy use, where is tha idea come from? If we do this by User.password pattern the Auth component will automatically hash the password value and this is happen before it’s value validated with User model, see dangerous right! Yes the answer is how do we know or model validation know that it’s value is more than 6 character or not whereas the value hashed and appear with a random long character so the effect password validation with minimal character 6 length will be return true always even if we fill an empty password. But this is will not happen for User.pwd or another than pattern above. But the problem next is our password is a plaintext it is not hashed, don’t worry it will be handle with beforeSave nodel callback.
input(‘User.pwd2’,array(‘label’=>__(‘Password again’,true),’type’=>’password’)); ?>
beforeSave callback is executed as soon as data validated with model but bofore this data saved by the model to database, cool this point.
So we can put our code here to hash our plaintext User.pwd and assign its value to $this->data[‘User’][‘password’] that is our fieldname in the users table, here is the code:
In our model usualy User model we have to place this code
data)) {See we use Security::hash($value,null,true) rather than $this->Auth->password() don’t worry its deliver a same value with Auth hash function, finally the first problem solved.
$this->data[‘User’][‘password’] = Security::hash($this->data[‘User’][‘pwd’]);
}
}
…..
}
?>
For the second we have to define a validation like this:
………..
‘pwd2’ => array(……….
‘rule’ => ‘matchWithPwd’,
‘message’ => ‘The confirmation password have to be matched’
).
Function matchWithPwd() {………………
If($this->data[‘User’][‘pwd’] == $this->data[‘User’][‘pwd2’]) {
Return true;
}else {
Return false;
}
}
Simple right?! Enjoy and have fun with this code above.
Tidak ada komentar:
Posting Komentar