How do I run validation checks on a password field in CakePHP, seeing that the password is hashed before I get a chance to run any checks on it?
It works this way for me (in the model):
public $validate = array(
'password' => array(
'minLength' => array(
'rule' => array('minLength', '8')
)
)
);
If you want to do more validations then create a custom validation method in the appropriate model. In the custom validation method hash password this way: Security::hash($this->data['User']['password'], null, true)
If you only have a single password
field in your form, you will need to create a custom hash function that either does nothing, or, better, preserves the original password somewhere.
Most likely though you have two password fields in your form where the user is required to confirm the password. In this case, you perform your password validation rules on the second password field. This can automatically happen in a custom validation rule, remember that you have access to all other fields inside a validation function via $this->data
. You can then confirm that the two passwords are identical as described here.