Skip to content

Model Configuration

validatesInclusionOf()

validatesInclusionOf() — returns void

Available in: model Category: Validation Functions

Ensures that a property’s value exists in a predefined list of allowed values. It is commonly used for dropdowns, radio buttons, or any scenario where only specific values are acceptable.

NameTypeRequiredDefaultDescription
propertiesstringnoName of property or list of property names to validate against (can also be called with the property argument).
liststringyesList of allowed values.
messagestringno[property] is not included in the listSupply 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. Ensure that the user selects either "Wheels" or "Rails" as their framework
validatesInclusionOf(
    property="frameworkOfChoice",
    list="wheels,rails",
    message="Please try again, and this time, select a decent framework!"
);

2. Validate multiple properties at once
validatesInclusionOf(
    properties="frameworkOfChoice,editorChoice",
    list="wheels,rails,vsCode,sublime",
    message="Invalid selection."
);

3. Only validate when creating a new object
validatesInclusionOf(
    property="subscriptionType",
    list="free,premium,enterprise",
    when="onCreate",
    message="You must choose a valid subscription type."
);

4. Skip validation if property is blank
validatesInclusionOf(
    property="preferredLanguage",
    list="cfml,python,javascript",
    allowBlank=true
);

5. Conditionally validate only for users in Europe
validatesInclusionOf(
    property="currency",
    list="EUR,GBP,CHF",
    condition="this.region eq 'Europe'",
    message="Invalid currency for European users."
);