Back
validatesExclusionOf()
Ensures that the value of a specified property is not included in a given list of disallowed values. This is commonly used to prevent reserved words, restricted entries, or disallowed values from being saved to the database. You can specify when the validation should run, allow blank values to skip validation, or conditionally run it.
1. Prevent users from selecting certain programming languages
validatesExclusionOf(
property="coolLanguage",
list="php,fortran",
message="Haha, you can not be serious. Try again, please."
);
2. Validate multiple properties at once
validatesExclusionOf(
properties="username,email",
list="admin,root,system",
message="This value is reserved. Please choose another."
);
3. Only apply validation on object creation
validatesExclusionOf(
property="username",
list="admin,root",
when="onCreate",
message="Username is reserved and cannot be used."
);
4. Skip validation if the property is blank
validatesExclusionOf(
property="nickname",
list="boss,chief",
allowBlank=true
);
5. Conditional validation using `condition`
validatesExclusionOf(
property="category",
list="deprecated,legacy",
condition="this.isArchived",
message="Archived items cannot use deprecated categories."
);
6. Skip validation for admin users using `unless`
validatesExclusionOf(
property="role",
list="banned,guest",
unless="this.isAdmin",
message="This role is restricted for regular users."
);
Copied!