Are you using AuthComponent? Keep in mind that it hashes all incoming password fields (but not “password confirmation”, check with debug($this->data) ), so the fields will never be the same. Read the manual and use AuthComponent::password to complete the verification.
Having said that, here I use:
public $validate = array( 'password' => array( 'confirm' => array( 'rule' => array('password', 'password_control', 'confirm'), 'message' => 'Repeat password', 'last' => true ), 'length' => array( 'rule' => array('password', 'password_control', 'length'), 'message' => 'At least 6 characters' ) ), 'password_control' => array( 'notempty' => array( 'rule' => array('notEmpty'), 'allowEmpty' => false, 'message' => 'Repeat password' ) ) ); public function password($data, $controlField, $test) { if (!isset($this->data[$this->alias][$controlField])) { trigger_error('Password control field not set.'); return false; } $field = key($data); $password = current($data); $controlPassword = $this->data[$this->alias][$controlField]; switch ($test) { case 'confirm' : if ($password !== Security::hash($controlPassword, null, true)) { $this->invalidate($controlField, 'Repeat password'); return false; } return true; case 'length' : return strlen($controlPassword) >= 6; default : trigger_error("Unknown password test '$test'."); } }
This is bad for the following reasons:
- It has a tight connection with the form, always expects the
password_control field to be there. You need to use the white list or disable the check if you do not have it, i.e.: $this->User->save($this->data, true, array('field1', 'field2')) . - Manually hashes the password as AuthComponent does (since there is no clean access to components from the model). If you change the algorithm used in AuthComponent, you also need to change it here.
Having said that, he transparently checks and creates the correct error messages for both the password and password management fields, without requiring additional code in the controller.
deceze
source share