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.

component extends="cbwire.models.Component" {
    
    // Data properties
    data = {
        "task": ""
    };
    
    // Validation constraints
    constraints = {
        "task": { "required": true, "requiredMessage": "Task is required"
    };
    
}

Manual Validation

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

validate

Returns a ValidateResult object.

component extends="cbwire.models.Component" {
    
    // Data properties
    data = {
        "task": ""
    };
    
    // Validation constraints
    constraints = {
        "task": { "required": true, "requiredMessage": "Task is required"
    };
    
    // Action
    function addTask() {
        var result = validate(); // ValidateResult object
        
        if ( !result.hasErrors() ) {
            // Do something
        }
    }
}

validateOrFail

Silently fails and prevents further processing of the current action.

component extends="cbwire.models.Component" {
    
    // Data properties
    data = {
        "task": ""
    };
    
    // Validation constraints
    constraints = {
        "task": { "required": true, "requiredMessage": "Task is required"
    };
    
    // Action
    function addTask() {
        validateOrFail(); // fail silently if validation doesn't pass.
        // Otherwise, continue
    }
}

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

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

In your templates, you have several helper methods for displaying errors.

You can use hasErrors() and getErrors() to check for validation errors that have occurred and to display all errors.

<div>
    Add Task
    <cfif hasErrors()>
        <div class="alert alert-danger">
            <ul>
                <cfloop array="#getErrors()#" index="error">
                    <li>#error#</li>
                </cfloop>
            </ul>
        </div>
    </cfif>
</div>

To check for errors on a specific data property, you can use hasError( field) and getError( field )

<div>
    <input type="text" wire:model="name">
    <cfif hasError( "name" )>
        <span class="text-danger">#getError( "name" )</span>
    </cfif>
</div>

Last updated