CBWIRE
InstallSource CodeIssuesSupport
v2.x
v2.x
  • Introduction
  • Release History
    • What's New With 2.2
    • What's New With 2.1
    • What's New With 2.0
  • Getting Started
  • Examples
  • Essentials
    • Configuration
    • Wires
    • Data Properties
    • Computed Properties
      • Computed Properties ( Proxied )
    • Actions
    • Templates
    • Events & Listeners
    • Wire Lifecycle
    • JavaScript
    • Testing
  • Wire Features
    • Validation
    • File Uploads
    • Query String
    • Redirecting
    • Dependency Injection
  • Template Features
    • Directives
    • Loading States
    • Polling
    • Prefetching
    • Offline State
    • Defer Loading
    • Dirty State
  • Integrations
    • ContentBox CMS
    • Turbo
    • AlpineJS
    • Inline Scripts
Powered by GitBook
On this page
  • Defining Constraints
  • Validating
  • validate
  • validateOrFail
  • Validation Manager
  • Displaying Errors

Was this helpful?

Export as PDF
  1. Wire Features

Validation

PreviousTestingNextFile Uploads

Last updated 2 years ago

Was this helpful?

include powerful validations that you can use to validate your .

Validations are provided by .

Defining Constraints

You can define constraints within your wires using this.constraints.

component extends="cbwire.models.Component"{

    this.constraints = {
        "task": { required: true }
    };
    
    // Data Properties
    data = {
        "task": ""
    };
}

Validating

can validate against the defined constraints using validate() or validateOrFail().

validate

Returns a ValidateResult object.

component extends="cbwire.models.Component"{
    // Action
    function addTask() {
        var result = validate(); // ValidateResult object
        
        if ( !result.hasErrors() ) {
            queryExecute( ... );
        }
    }
}

validateOrFail

component extends="cbwire.models.Component"{
    // Action
    function addTask() {
        validateOrFail();

        queryExecute( ... );        
    }
}

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 args.validation.

If you need more granular control over the validation response, use validate() instead.

Validation Manager

You can get a new ValidationManager object to work with, just call getValidationManager();

component extends="cbwire.models.Component"{
    // Action
    function addTask() {
        var data = { "email": "cbwire@rocks.com" };

        var validationManager = getValidationManager();
        
        var result = validationManager.validate(
            target=data,
            constraints={
                "email": { required: true, type: "email" }
            }
        );
    }
}

Displaying Errors

<cfoutput>
<div>
    <input wire:model="task" type="text">
    <button wire:click="addTask">Add</button>
    
    <cfif args.validation.hasErrors( "task" )>
        <cfloop array="#args.validation.getAllErrors( "task" )#" index="error">
            <div>#error#</div>
        </cfloop>
    </cfif>
</div>
</cfoutput>

Silently fails and prevents further processing of the current .

can access the ValidationResults object using args.validation. This includes helpful methods you can use for displaying error messages.

Wires
Data Properties
cbValidation
Actions
Action
Templates