Add confirm password with validation in CakePHP


Creating a confirm password field with validation is be very easy with CakePHP.

By default there’s not confirm password that is set in cakePHP builder. You can achieve this by modifying the User’s model password validation. This will allow us to override certain functionalities and adds up our own.

In your User model password validation can be something like below.
By default the above code is the structure for validating a password field, if it is empty.
The ‘password‘ is the field in your database, while the ‘notEmpty‘ is just an alias for a certain rule. Now you can change it name you want but the default is ‘notEmpty‘. To add a custom rule we will add a new alias and we will call it matchPasswords so that the code above will be something like below.
Notice that we have created a certain rule name matchPasswords that will call a function outside our validation. The matchPasswords function will just simply take the data from the view which is the confirm_password input field and then compare it to the password field. If it matches, it will proceed otherwise it will bring a not equal password message.
That’s it. Now in your User’s view you can have something like below and your good to go! The confirm password will now properly triggered.

Sharing is caring!