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 in your model constraints like so:
1
{ fieldName : { validator: "[email protected]" } }
Copied!
That's it! Once you define a property with this validator, then cbValidation will delegate to the cborm UniqueValidator so it can identify if the value is unique in the database. Below you can see an example entity that marks the userName property as unique for validation purposes.
models/User.cfc
1
component persistent="true" table="users"{
2
3
property name="id" column="user_id" fieldType="id" generator="uuid";
4
property name="firstName";
5
property name="lastName";
6
property name="userName" unique="true";
7
property name="password";
8
property name="lastLogin" ormtype="date";
9
10
// M20 -> Role
11
property name="role" cfc="Role" fieldtype="many-to-one" fkcolumn="FKRoleID" lazy="true" notnull="false";
12
13
// DI Test
14
property name="testDI" inject="model:testService" persistent="false" required="false";
15
16
// Validation Constraints
17
this.constraints = {
18
"firstName" : { required = true },
19
"lastName" : { required = true },
20
"userName" : { required = true, validator="[email protected]" }
21
};
22
23
}
Copied!
Now here is a sample validation:
handlers/users.cfc
1
component{
2
3
property name="userService" inject="entityService:User";
4
5
function create( event, rc, prc ){
6
var oUser = populateModel( "User" );
7
var vResults = validateModel( oUser );
8
9
if( !vResults.isValid() ){
10
return event
11
.setHTTPHeader( 400, "Invalid Data" )
12
.renderData( type="json", data=vResults.getAllErrorsAsStruct() );
13
}
14
15
userService.save( oUser );
16
17
return oUser.getMemento();
18
}
19
20
21
}
Copied!
Last modified 2yr ago
Copy link