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.

Name Type Required Default Description
properties string No Name of property or list of property names to validate against (can also be called with the property argument).
message string No [property] should match confirmation Supply a custom error message here to override the built-in one.
when string No onSave Pass in onCreate or onUpdate to limit when this validation occurs (by default validation will occur on both create and update, i.e. onSave).
condition string No String expression to be evaluated that decides if validation will be run (if the expression returns true validation will run).
unless string No String expression to be evaluated that decides if validation will be run (if the expression returns false validation will run).
caseSensitive boolean No false Ensure the confirmed property comparison is case sensitive
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!