Back
validatesConfirmationOf()
Validates that the value of the specified property also has an identical confirmation value.
This is common when having a user type in their email address a second time to confirm, confirming a password by typing it a second time, etc.
The confirmation value only exists temporarily and never gets saved to the database.
By convention, the confirmation property has to be named the same as the property with "Confirmation" appended at the end.
Using the password example, to confirm our password property, we would create a property called passwordConfirmation
.
1. Validate password confirmation on user creation
validatesConfirmationOf(
property="password",
when="onCreate",
message="Your password and its confirmation do not match. Please try again."
);
2. Validate multiple fields at once: password and email
validatesConfirmationOf(
properties="password,email",
when="onCreate",
message="Fields must match their confirmation."
);
3. Case-sensitive validation
validatesConfirmationOf(
property="password",
caseSensitive=true,
message="Password and confirmation must match exactly, including case."
);
4. Conditional validation using `condition`
validatesConfirmationOf(
property="email",
condition="this.isNewsletterSubscriber",
message="Email confirmation required for newsletter subscription."
);
5. Skip validation for admin users using `unless`
validatesConfirmationOf(
property="password",
unless="this.isAdmin",
message="Admins do not need to confirm their password."
);
Copied!