Skip to content

Model Configuration

validatesExclusionOf()

validatesExclusionOf() — returns void

Available in: model Category: Validation Functions

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.

NameTypeRequiredDefaultDescription
propertiesstringnoName of property or list of property names to validate against (can also be called with the property argument).
liststringyesSingle value or list of values that should not be allowed.
messagestringno[property] is reservedSupply a custom error message here to override the built-in one.
whenstringnoonSavePass in onCreate or onUpdate to limit when this validation occurs (by default validation will occur on both create and update, i.e. onSave).
allowBlankbooleannofalseIf set to true, validation will be skipped if the property value is an empty string or doesn’t exist at all. This is useful if you only want to run this validation after it passes the validatesPresenceOf test, thus avoiding duplicate error messages if it doesn’t.
conditionstringnoString expression to be evaluated that decides if validation will be run (if the expression returns true validation will run).
unlessstringnoString expression to be evaluated that decides if validation will be run (if the expression returns false validation will run).
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."
);