> For the complete documentation index, see [llms.txt](https://cbwire.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cbwire.ortusbooks.com/3.x/wire-features/validation.md).

# Validation

Powerful validations for your forms and input elements.

CBWIRE includes powerful validations provided by [cbValidation](https://coldbox-validation.ortusbooks.com/) that you can use to validate your [Properties](https://cbwire.ortusbooks.com/3.x/essentials/properties.md).

## 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
```

{% hint style="info" %}
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.
{% endhint %}

## 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.

<pre class="language-html"><code class="lang-html">&#x3C;cfscript>
    constraints = {
        "task": { required: true }
    };
    
    // Data Properties
    data = {
        "task": ""
    };
&#x3C;/cfscript>

<strong>&#x3C;cfoutput>
</strong>    &#x3C;div>
        &#x3C;!-- HTML goes here -->
    &#x3C;/div>
&#x3C;/cfoutput>
</code></pre>

## Manual Validation

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

### validate

Returns a *ValidateResult* object.

```html
<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.

```html
<cfscript>
    constraints = {
        "task": { required: true }
    };

    // Action
    function addTask() {
        validateOrFail();
        // Continue if validation passes
    }
</cfscript>

<cfoutput>
    <div>
        <!-- HTML goes here -->
    </div>
</cfoutput>
```

{% hint style="info" %}
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.
{% endhint %}

## Explicit Constraints

If you can also run validations against any constraints you provide by passing them to the *validate()* method.

```html
<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.

```html
<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>
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://cbwire.ortusbooks.com/3.x/wire-features/validation.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
