Links
Comment on page

Validation

Powerful validations for your forms and input elements.
CBWIRE includes powerful validations provided by cbValidation that you can use to validate your Properties.

Installation

You'll need to install cbValidation to use the validation features with CBWIRE.
To install cbValidation, run the following command in CommandBox:
box install cbvalidation
Previous versions of CBWIRE included cbValidation automatically. However, this introduced issues if you wanted to use a module version different from CBWIRE's version, so you now must manually install cbValidation.

Defining Constraints

You can define constraints within your components by populating a constraints struct. Once constraints are defined, CBWIRE will automatically validate your component on each request.
<cfscript>
constraints = {
"task": { required: true }
};
// Data Properties
data = {
"task": ""
};
</cfscript>
<cfoutput>
<div>
<!-- HTML goes here -->
</div>
</cfoutput>

Manual Validation

You can manually validate your component using the validate() or validateOrFail() methods.

validate

Returns a ValidateResult object.
<cfscript>
constraints = {
"task": { required: true }
};
// Action
function addTask() {
var result = validate(); // ValidateResult object
if ( !result.hasErrors() ) {
// Do something
}
}
</cfscript>
<cfoutput>
<div>
<!-- HTML goes here -->
</div>
</cfoutput>

validateOrFail

Silently fails and prevents further processing of the current action.
<cfscript>
constraints = {
"task": { required: true }
};
// Action
function addTask() {
validateOrFail();
// Continue if validation passes
}
</cfscript>
<cfoutput>
<div>
<!-- HTML goes here -->
</div>
</cfoutput>
With validateOrFail(), the error is gracefully caught and any further processing of the invoked action is prevented. The XHR response and re-rendered template are still returned. The actual errors themselves are available to the template using the validation variable.
If you need more granular control over the response, use validate() instead.

Explicit Constraints

If you can also run validations against any constraints you provide by passing them to the validate() method.
<cfscript>
// Action
function addTask() {
var result = validate(
target=data,
constraints={
"task": { required: true }
}
); // ValidateResult object
if ( !result.hasErrors() ) {
// Do something
}
}
</cfscript>
<cfoutput>
<div>
<!-- HTML goes here -->
</div>
</cfoutput>

Displaying Errors

You can access the ValidationResults object using the validation variable. This includes helpful methods you can use for displaying error messages.
<cfscript>
// ....
</cfscript>
<cfoutput>
<div>
<input wire:model="task" type="text">
<button wire:click="addTask">Add</button>
<cfif validation.hasErrors( "task" )>
<cfloop array="#validation.getAllErrors( "task" )#" index="error">
<div>#error#</div>
</cfloop>
</cfif>
</div>
</cfoutput>