Validation

Validation Functions

Our active entity object will also give you access to our validation engine (cbValidation) by giving your ORM entities the following functions:
1
/**
2
* Validate the ActiveEntity with the coded constraints -> this.constraints, or passed in shared or implicit constraints
3
* The entity must have been populated with data before the validation
4
*
5
* @fields One or more fields to validate on, by default it validates all fields in the constraints. This can be a simple list or an array.
6
* @constraints An optional shared constraints name or an actual structure of constraints to validate on.
7
* @locale An optional locale to use for i18n messages
8
* @excludeFields An optional list of fields to exclude from the validation.
9
*/
10
boolean function isValid(
11
string fields="*",
12
any constraints="",
13
string locale="",
14
string excludeFields=""
15
){
16
17
/**
18
* Get the validation results object. This will be an empty validation object if isValid() has not being called yet.
19
*/
20
cbvalidation.models.result.IValidationResult function getValidationResults(){
Copied!

Declaring Constraints

This makes it really easy for you to validate your ORM entities in two easy steps:
1) Add your validation constraints to the entity
2) Call the validation methods
Let's see the entity code so you can see the constraints:
models/User.cfc
1
component persistent="true" extends="cborm.models.ActiveEntity"{
2
3
// Properties
4
property name="firstName";
5
property name="lastName";
6
property name="email";
7
property name="username";
8
property name="password";
9
10
// Validation Constraints
11
this.constraints = {
12
"firstName" = {required=true},
13
"lastName" = {required=true},
14
"email" = {required=true,type="email"},
15
"username" = {required=true, size="5..10"},
16
"password" = {required=true, size="5..10"}
17
};
18
}
Copied!

Validating Constraints

Now let's check out the handlers to see how to validate the entity via the isValid() function:
handlers/users.cfc
1
component{
2
3
property name="messagebox" inject="[email protected]";
4
5
function index(event,rc,prc){
6
prc.users = getInstance( "User" ).list( sortOrder="lastName asc" );
7
event.setView( "users/list" );
8
}
9
10
function save(event,rc,prc){
11
event.paramValue( "id", -1 );
12
13
var oUser = getInstance( "User" )
14
.getOrFail( rc.id )
15
.populate( rc )
16
17
if( oUser.isValid() {
18
oUser.save();
19
flash.put( "notice", "User Saved!" );
20
relocate( "users.index" );
21
}
22
else{
23
messagebox.error( messageArray=oUser.getValidationResults().getAllErrors() );
24
editor( event, rc, prc );
25
}
26
27
}
28
29
function editor(event,rc,prc){
30
event.paramValue( "id", -1 );
31
prc.user = getInstance( "User" ).getOrFail( rc.id );
32
event.setView( "users/editor" );
33
}
34
35
function delete(event,rc,prc){
36
event.paramValue( "id", -1 );
37
getInstance( "User" )
38
.deleteById( rc.id );
39
flash.put( "notice", "User Removed!" );
40
relocate( "users.index" );
41
}
42
}
Copied!
Please remember that the isValid() function has several arguments you can use to fine tune the validation:
  • fields
  • constraints
  • locale
  • excludeFields

Displaying Errors

You can refer back to the cbValidation docs for displaying errors:
Page not found
CB Validation Module
Here are the most common methods for retreving the errors from the Result object via the getValidationResults() method:
  • getResultMetadata()
  • getFieldErrors( [field] )
  • getAllErrors( [field] )
  • getAllErrorsAsJSON( [field] )
  • getAllErrorsAsStruct( [field] )
  • getErrorCount( [field] )
  • hasErrors( [field] )
  • getErrors()
The API Docs in the module (once installed) will give you the latest information about these methods and arguments.

Unique Property Validation

We have also integrated a UniqueValidator from the validation module into our ORM module. It is mapped into WireBox as [email protected] so you can use it in your model constraints like so:
1
{ username : { validator : "[email protected]", required : true } }
Copied!
Last modified 2yr ago