Domain Object
Within any domain object you can define a public variable called this.constraints that is a assigned an implicit structure of validation rules for any fields or properties in your object.

Declaration

models/User.cfc
1
component persistent="true"{
2
3
// Object properties
4
property name="id" fieldtype="id" generator="native" setter="false";
5
property name="fname";
6
property name="lname";
7
property name="email";
8
property name="username";
9
property name="password";
10
property name="age";
11
12
// Validation
13
this.constraints = {
14
// Constraints go here
15
}
16
}
Copied!
We can then create the validation rules for the properties it will apply to it:
config/User.cfc
1
component persistent="true"{
2
3
...
4
5
// Validation
6
this.constraints = {
7
fname = { required = true },
8
lname = { required = true},
9
username = {required=true, size="6..10"},
10
password = {required=true, size="6..8"},
11
email = {required=true, type="email"},
12
age = {required=true, type="numeric", min=18}
13
};
14
}
Copied!
That easy! You can just declare these validation rules and ColdBox will validate your properties according to the rules. In this case you can see that a password must be between 6 and 10 characters long, and it cannot be blank.
By default all properties are of type string and not required

Usage

You can then use them implicitly when calling our validation methods:
1
validate( myUser );
2
validateOrFail( myUser );
Copied!
Last modified 1yr ago
Copy link