In cbValidation 1.5 we introduced the validateOrFail()
function. This function works in similar manner to the validate()
method, but instead of giving you the results object, it throws an exception.
So your validation fails, where are the results? In the exception structure under the extendedInfo
key. We store the validation results as JSON in the extended info and then you can use them for display purposes:
Incoming Target
Validation Fails
Result
Object
false
Returns the same object
Object
true
Throws ValidationException
Struct
false
Returns the structure with ONLY the fields that were validated from the constraints
Struct
true
Throws ValidationException
We also have the ability to validate a target object with custom a-la-carte constraints by passing the constraints inline as an struct of structs. This way you can store these constraint rules anywhere you like.
This will validate the object using the inline constraints that you built.
We also have the ability to validate a target object or form with shared constraints from our configuration file. Just use the name of the key in the configuration form as the name of the constraints
argument.
This will validate the object and rc
using the sharedUser
constraints.
You can also tell the validation manager to ONLY validate on certain fields and not all the fields declared in the validation constraints.
This will only validate the login
and password
fields.
Most likely you will be validating your objects at the controller layer in your ColdBox event handlers. All event handlers,layouts, views and interceptors have some new methods thanks to our module mixins.
You pass in your target object or structure, an optional list of fields or properties to validate only (by default it does all of them), an an optional constraints argument which can be the shared name or an actual constraints structure a-la-carte. If no constraints are passed, then we will look for the constraints in the target object as a public property called constraints
. The validate()
method returns a cbvalidation.models.results.IValidationResult
type object, which you can then use for evaluating the validation.
The return of validate model is our results interface which has cool methods like:
Some of these methods return error objects which adhere to our Error Interface: cbvalidation.models.result.IValidationError
, which can quickly tell you what field had the exception, what was the rejected value and the validation message: