By default if a constraint fails an error message will be set in the result objects for you in English. If you would like to have your own custom messages for specific constraints you can do so by following the constraint message convention:
Just add the name of the constraint you like and append to it the work Message and you are ready to roll:
We also setup lots of global {Key}
replacements for your messages and also several that the core constraint validators offer as well. This is great for adding these customizations on your custom messages and also your i18n messages (Keep Reading):
{rejectedValue}
- The rejected value
{field or property}
- The property or field that was validated
{validationType}
- The name of the constraint validator
{validationData}
- The value of the constraint definition, e.g size=5..10, then this value is 5..10
{DiscreteValidator}
- operation, operationValue
{InListValidator}
- inList
{MaxValidator}
- max
{MinValidator}
- min
{RangeValidator}
- range, min, max
{RegexValidator}
- regex
{SameAsValidator}
, {SameAsNoCaseValidator}
- sameas
{SizeValidator}
- size, min, max
{TypeValidator}
- type
Below are all the currently supported constraints. If you need more you can create your own Custom validators.
With the validator
constraint you can specify your own custom validator, but if you need your own parameters for your validator this is a bit limited. You can also specify YourOwnValidator
as constraint label where YourOwnValidator
is a wirebox id string. In this case you can specify your own parameters.
See Advanced Custom Validators for details.
WARNING: You can't do a normal wirebox mapping for YourOwnValidator
in your main application. A validator needs an IValidator
interface from the cbvalidation
module. When wirebox inspects the binder, the cbvalidation
module is not loaded yet, so it will error. This can be solved by defining your custom validators in an own module (depending on cbvalidation
) or by mapping your validator in the afterConfigurationLoad()
method of your binder, e.g in config/wirebox.cfc
Constraint
Type
Default
required
boolean
false
Whether the property must have a non-null value
type
string
string
Validates that the value is of a certain format type. Our included types are: ssn,email,url,alpha,boolean,date,usdate,eurodate,numeric,GUID,UUID,integer,string,telephone,zipcode,ipaddress,creditcard,binary,component,query,struct,json,xml
size
numeric or range
---
The size or length of the value which can be a struct, string, array, or query. The value can be a single numeric value or our cool ranges. Ex: size=4, size=6..8, size=-5..0
range
range
---
Range is a range of values the property value should exist in. Ex: range=1..10, range=6..8
regex
regular expression
---
The regular expression to try and match the value with for validation. This is a no case regex check.
sameAs
propertyName
---
Makes sure the value of the constraint is the same as the value of another property in the object. This is a case sensitive check.
sameAsNoCase
propertyName
---
Makes sure the value of the constraint is the same as the value of another property in the object with no case sensitivity.
inList
string list
---
A list of values that the property value must exist in
discrete
string
---
Do discrete math in the property value. The valid values are: eq,neq,lt,lte,gt,gte. Example: discrete="eq:4" or discrete="lte:10"
udf
UDF or closure
---
I can do my own custom validation by doing an inline closure (CF 10 or Railo only) or a pointer to a custom defined function. The function must return boolean and accepts two parameters: value and target.
method
method name
---
The name of a method to call in the target object for validation. The function must return boolean and accepts two parameters: value and target.
min
numeric
---
The value must be greater than or equal to this minimum value
max
numeric
---
The value must be less than or equal to this maximum value
validator
instantiation path or wirebox DSL
---
You can also build your own validators instead of our internal ones. This value will be the instantiation path to the validator or a wirebox id string. Example: validator="mymodel.validators.MyValidator", validator="id:MyValidator"
The unique
constraint is part of the cborm module. So make sure that the cborm
module is installed first.
See the Advanced Custom Validators for a uniqueness validator which is not dependent of ORM
The constraints is mapped into WireBox as UniqueValidator@cborm
so you can use in your constraints like so:
If you will be using the unique constraint, then the name of the property has to be EXACTLY the same case as the constraint name. To do this, use single or double quotes to declare the constraint name. Please see example below.