CakePHP 3 - Compare Passwords

I have two password fields (this field is in the database) and confirm_password (this field is not in the database)

Well, I need to compare if the password == confirm_password .. but I do not know what to create a user check for "confirm_password" ... Do I need to have this field in the database?

How do i do

+7
php cakephp
source share
2 answers

As a rule, you can access all the data in a custom validation rule using the $context argument, where it is stored in the data key, i.e. $context['data']['confirm_password'] , which can then be compared with the current value of the field.

 $validator->add('password', 'passwordsEqual', [ 'rule' => function ($value, $context) { return isset($context['data']['confirm_password']) && $context['data']['confirm_password'] === $value; } ]); 

Recently, the compareWith validation rule has been introduced, which does just that.

https://github.com/cakephp/cakephp/pull/5813

 $validator->add('password', [ 'compare' => [ 'rule' => ['compareWith', 'confirm_password'] ] ]); 
+22
source share

Now there is a call sameAs method in the validation class, for version 3.2 or grater.

  $validator -> sameAs('password_match','password','Passwords not equal.'); 

see API

+2
source share

All Articles