I am working on Cake 3. I want to create a custom validation-rule. I want to check if the field 'password' is equal to the 'confirm_password' field.
This is my code:
public function validationDefault(Validator $validator) {
$validator
->add('id', 'valid', ['rule' => 'numeric'])
->allowEmpty('id', 'create')
->add('email', 'valid', ['rule' => 'email'])
->requirePresence('email', 'create')
->notEmpty('email')
->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table'])
->requirePresence('password', 'create')
->notEmpty('password')
->notEmpty('confirm_password')
->add('confirm_password', 'custom', [
'rule' => function($value, $context) {
if ($value !== $context['data']['password']) {
return false;
}
return false;
},
'message' => 'The passwords are not equal',
]);
return $validator;
}
When I try to 'fail' the form-submit, the code saves, and I get no error.
I read http://book.cakephp.org/3.0/en/core-libraries/validation.html#custom-validation-rules but didn't help.... Anybody?
Thanks!
Another built in way to compare two passwords with CakePHP 3 validation could be:
->add('confirm_password',
'compareWith', [
'rule' => ['compareWith', 'password'],
'message' => 'Passwords not equal.'
]
)
You can also add this to your validationDefault
method in your Table definition.