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 of type ValidationException
.
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:
cbValidation 2.x series introduced the ability to validate using field profiles
. This will allow you to define all your constraints but also define field profiles where you can define only certain fields to be validated if the profile name is used.
This is using the this.constraintProfiles
struct literal:
The key is the name of the profile and the value is a list of the fields to validate if the profile is targeted for validation.
Every validation method: validate(), validateOrFail()
has a profiles
argument. You can then pass one or more to the argument so you can validate 1 or more profiles:
validate(), validateOrFail()
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), and 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 and can be found under cbvalidation.models.result.IValidationResult
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:
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 defined in the config/Coldbox.cfc
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.
You can also use the following arguments:
includeFields
: The fields to include in the validation ONLY
excludeFields
: The fields to exclude in the validation
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