Available Constraints
Below are all the currently supported constraints. If you need more you can create your own Custom validators as well.
accepted
The field must be yes, on, 1, or true. This is useful for validating "Terms of Service" acceptance. Note: This validator will ignore values that are null or empty strings.
after
The field under validation must be a value after a given date. The dates will be passed into the dateCompare()
function in order to be converted and tested. Note: This validator will ignore values that are null or empty strings.
Instead of passing a date, you may specify another field to compare against the date as well:
afterOrEqual
The field under validation must be a value after or equal a given date. The dates will be passed into the dateCompare()
function in order to be converted and tested. Note: This validator will ignore values that are null or empty strings.
alpha
The field must be alphabetical ONLY. Note: This validator will ignore values that are null or empty strings.
arrayItem
This validator is used to validate an array's items. It will iterate through each of the array's items and validate each item against the validationData
constraints you pass in. Note: This validator will ignore values that are null or empty strings.
You may also specify items
as an alias to arrayItem
.
Any validation errors found will be named using the parent field name and array index.
You can validate nested structs by nesting a constraints
validator.
There is a shortcut notation available for arrayItem
that uses a specialized field name to skip nesting the constraints.
before
The field under validation must be a value before a given date. The dates will be passed into the dateCompare()
function in order to be converted and tested. Note: This validator will ignore values that are null or empty strings.
Instead of passing a date, you may specify another field to compare against the date as well:
beforeOrEqual
The field under validation must be a value before or equal a given date. The dates will be passed into the dateCompare()
function in order to be converted and tested. Note: This validator will ignore values that are null or empty strings.
constraints
This validator is used to validate a nested struct. The value of this validator are the constraints for the nested struct. Note: This validator will ignore values that are null.
Any validation errors found will be named using the parent field name and the child field name.
constraints
can be used as many levels deep as you need to go.
constraints
can also be combined with items
to validate an array of structs.
There is a shortcut notation available for constraints
that uses a specialized field name to skip nesting the constraints.
dateEquals
The field under validation must be a value that is the same as the given date. The dates will be passed into the dateCompare()
function in order to be converted and tested. Note: This validator will ignore values that are null or empty strings.
Instead of passing a date, you may specify another field to compare against the date as well:
discrete
The field must pass certain discrete math operations using the format: operator:value
Note: This validator will ignore values that are null or empty strings.
gt
- Greater than the valuegte
- Greater than or equal to the valuelt
- Less than the valuelte
- Less than or equal to the valueeq
- Equal to the valueneq
- Not equal to the value
empty
The field is not required but if it exists it cannot be empty. Note: This validator will ignore values that are null.
This is needed since required validators allow empty strings when false
while type validators ignore empty values as valid. This means we can have a situation as follows:
With these validation rules passing in startDate = ""
would pass the validation! The empty validator helps us ensure that the value passed in is not empty (and, in this case, a date).
The field still isn't required, but if it is passed the value must be a non-empty value and it must be parseable as a date.
inList
The field must be in the included list. Note: This validator will ignore values that are null or empty strings.
instanceOf
The value passed must be an instance of a particular type. Note: This validator will ignore values that are null or empty strings.
items
See arrayItem.
max
The field must be less than or equal to the defined value. Note: This validator will ignore values that are null or empty strings.
method
The methodName
will be called on the target object and it will pass in validationData, targetValue, and metadata. It must return a boolean response: true = pass, false = fail.
Any data you place in the metadata
structure will be set in the validation result object for later retrieval. Note: This validator will ignore values that are null or empty strings.
min
The field must be greater than or equal to the defined value. Note: This validator will ignore values that are null or empty strings.
nestedConstraints
See constraints.
notSameAsNoCase
The field must NOT be the same as another field with no case sensitivity. Note: This validator will ignore values that are null or empty strings.
notSameAs
The field must NOT be the same as another field with case sensitivity. Note: This validator will ignore values that are null or empty strings.
range
The field must be within the range values and the validation data must follow the range pattern: min..max
. Note: This validator will ignore values that are null or empty strings.
regex
The field must pass the regular expression match with no case sensitivity. Note: This validator will ignore values that are null or empty strings.
required
The field must have some type of value and not null or an empty string.
requiredIf
The field under validation must be present and not empty if the anotherfield
field is equal to the passed value
. The validation data can be a struct
or a string
representing the field to check, or it can be a UDF/closure/lambda to use for validation. The UDF must return boolean, validate( value, target, metadata ):boolean
Any data you place in the metadata
structure will be set in the validation result object for later retrieval.
requiredUnless
The field under validation must be present and not empty unless the anotherfield
field is equal to the passed value
. The validation data can be a struct
or a string
representing the field to check.
sameAsNoCase
The field must be the same as another field with no case sensitivity. Note: This validator will ignore values that are null or empty strings.
sameAs
The field must be the same as another field with case sensitivity. Note: This validator will ignore values that are null or empty strings.
size
The field value size must be within the range values and the validation data must follow the range pattern: min..max.
Value can be a (struct,string,array,query). Note: This validator will ignore values that are null or empty strings.
type
One of the most versatile validators. It can test if the value is of the following specific types:
alpha
array
binary
boolean
component
creditcard
date
email
eurodate
float
GUID
integer
ipaddress
json
numeric
query
ssn
string
struct
telephone
url
usdate
UUID
xml
zipcode
Note: This validator will ignore values that are null or empty strings.
udf
The field value, the target object, and an empty metadata structure will be passed to the declared closure/lambda to use for validation. The UDF must return boolean, validate( value, target, metadata ):boolean
Any data you place in the metadata
structure will be set in the validation result object for later retrieval. Note: This validator will ignore values that are null or empty strings.
unique
The field must be a unique value in a specific database table. The validation data is a struct with the following keys:
table
: The name of the table to checkcolumn
: The column to check, defaults to the property field in check
Note: This validator will ignore values that are null or empty strings.
validator
The field value will be passed to the validator CFC to be used for validation. Please see Custom Validators
Last updated