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.
/*** Validate an object or structure according to the constraints rules.* @target An object or structure to validate* @fields The fields to validate on the target. By default, it validates on all fields* @constraints A structure of constraint rules or the name of the shared constraint rules to use for validation* @locale The i18n locale to use for validation messages* @excludeFields The fields to exclude in the validation* * @return cbvalidation.model.result.IValidationResult*/functionvalidate()/** * Validate an object or structure according to the constraints rules and throw an exception if the validation fails. * The validation errors will be contained in the `extendedInfo` of the exception in JSON format * * @target An object or structure to validate * @fields The fields to validate on the target. By default, it validates on all fields * @constraints A structure of constraint rules or the name of the shared constraint rules to use for validation * @locale The i18n locale to use for validation messages * @excludeFields The fields to exclude from the validation * @includeFields The fields to include in the validation * * @return The validated object or the structure fields that where validated * @throws ValidationException */functionvalidateOrFail(){returngetValidationManager().validateOrFail( argumentCollection=arguments );}/*** Retrieve the application's configured Validation Manager*/functiongetValidationManager()
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.
functionsaveUser(event,rc,prc){// create and populate a user object from an incoming formvar user =populateModel( entityNew("User") );// validate modelprc.validationResults =validate( user );if( prc.validationResults.hasErrors() ){// show errors }else{// save }}
The return of validate model is our results interface which has cool methods like:
interface{/** * Add errors into the result object * @error.hint The validation error to add into the results object */ IValidationResult functionaddError(required IValidationError error);/** * Set the validation target object name */ IValidationResult functionsetTargetName(required string name);/** * Get the name of the target object that got validated */ string functiongetTargetName();/** * Get the locale */ string functiongetLocale();/** * has locale information */ boolean functionhasLocale();/** * Set the validation locale */ IValidationResult functionsetLocale(required string locale);/** * Determine if the results had error or not * @field.hint The field to count on (optional) */ boolean functionhasErrors(string field);/** * Clear All errors */ IValidationResult functionclearErrors();/** * Get how many errors you have * @field.hint The field to count on (optional) */ numeric functiongetErrorCount(string field);/** * Get the Errors Array, which is an array of error messages (strings) * @field.hint The field to use to filter the error messages on (optional) */ array functiongetAllErrors(string field);/** * Get an error object for a specific field that failed. Throws exception if the field does not exist * @field.hint The field to return error objects on */ IValidationError[] functiongetFieldErrors(required string field);/** * Get a collection of metadata about the validation results */ struct functiongetResultMetadata();/** * Set a collection of metadata into the results object */ IValidationResult functionsetResultMetadata(required struct data);}
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:
/*** Set error metadata that can be used in i18n message replacements or in views* @data.hint The name-value pairs of data to store in this error.*/IValidationError functionsetErrorMetadata(required any data);/*** Get the error metadata*/struct functiongetErrorMetadata();/*** Set the error message* @message.hint The error message*/IValidationError functionsetMessage(required string message);/*** Set the field* @message.hint The error message*/IValidationError functionsetField(required string field);/*** Set the rejected value* @value.hint The rejected value*/IValidationError functionsetRejectedValue(required any value);/*** Set the validator type name that rejected* @validationType.hint The name of the rejected validator*/IValidationError functionsetValidationType(required any validationType);/*** Get the error validation type*/string functiongetValidationType();/*** Set the validator data* @data.hint The data of the validator*/IValidationError functionsetValidationData(required any data);/*** Get the error validation data*/string functiongetValidationData();/*** Get the error message*/string functiongetMessage();/*** Get the error field*/string functiongetField();/*** Get the rejected value*/any functiongetRejectedValue();