Custom Validators
If the core validators are not sufficient for you, then you can create your own custom validators. You can either leverage the udf validator and create your own closure/lambda to validate inline or create a reusable validator CFC
Closure/Lambda Validator
If you use the udf validator, then you can declare your validation inline. Just create a closure/lambda that will be called for you at the time of validation. This closure/lambda will receive all the following arguments and MUST return a boolean indicator: true => passed, false => invalid
value: The value to validate, can be nulltarget: The object that is the target of validation
slug : {
required : true,
udf : ( value, target ) => {
if( isNull( arguments.value ) ) return false;
return qb.from( "content" )
.where( "slug", arguments.value )
.when( this.isLoaded(), ( q ) => {
arguments.q.whereNotIn( "id", this.getId() );
} )
.count() == 0;
}
},Custom CFC Validator
You can also create a reusable CFC that can be shared in any ColdBox app as a validator. Create the CFC and it should implement our interface which can be found here: cbvalidation.models.validators.IValidator and it specifies just two functions your own validator must implement: getName(), validate():
Here is a sample validator:
Defining Custom Validators
You can use them in two approaches when defining them in your constraints:
Use the
validatorconstraints which points to the Wirebox ID of your own custom validator object. Please note that if you use this approach you will not be able to pass validation data into the validator.Use the WireBox ID as they key of your validator. Then you can pass your own validation data into the validator.
Approach number 2 is much more flexible as it will allow you to declare multiple custom validators and each of those validators can receive validation data as well.
If you don't have any validation data to pass to a validator, just pass an empty struct ({}) or an empty string
Error Metadata Integration
Starting with CBValidation 4.3.0, both UDF and Method validators support error metadata - a powerful feature that allows custom validators to provide additional contextual information about validation failures. This metadata can be used for i18n message replacements, enhanced error display, or custom error handling logic.
How Error Metadata Works
When using UDF or Method validators, your validation function receives an additional errorMetadata argument (passed by reference) that you can populate with custom data when validation fails.
Accessing Error Metadata
Once validation fails and metadata is populated, you can access it through the ValidationError object:
Error Metadata in i18n Messages
Error metadata integrates seamlessly with CBValidation's i18n support. You can reference metadata values in your resource bundle messages:
ValidationError Metadata API
The ValidationError object provides methods to work with error metadata:
Best Practices for Error Metadata
Use Consistent Structure: Establish consistent metadata keys across your validators for easier handling
Meaningful Categories: Use category/type fields to enable different error handling logic
Include Context: Add relevant business context like IDs, dates, or limits that help with error resolution
Avoid Sensitive Data: Don't include passwords or other sensitive information in metadata
Document Your Schema: If building reusable validators, document what metadata fields they provide
Pro Tip: Use error metadata to create more user-friendly error messages and implement sophisticated error handling logic without modifying core validation behavior.
Last updated
Was this helpful?