> 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/v2-1/wire-features/validation.md).

# Validation

[Wires](/v2-1/essentials/creating-components.md) include powerful validations that you can use to validate your [Data Properties](/v2-1/essentials/properties.md).

{% hint style="info" %}
Validations are provided by [cbValidation](https://coldbox-validation.ortusbooks.com/).
{% endhint %}

## Defining Constraints

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

```javascript
component extends="cbwire.models.Component"{

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

## Validating

[Actions](/v2-1/essentials/actions.md) can validate against the defined constraints using `validate()` or `validateOrFail()`.

### validate

Returns a `ValidateResult` object.

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

### validateOrFail

Silently fails and prevents further processing of the current [Action](/v2-1/essentials/actions.md).&#x20;

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

        queryExecute( ... );        
    }
}
```

{% 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 `args.validation`.&#x20;

If you need more granular control over the validation response, use `validate()` instead.
{% endhint %}

## Validation Manager

You can get a new *ValidationManager* object to work with, just call `getValidationManager()`;&#x20;

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

[Templates](/v2-1/essentials/templates.md) can access the *ValidationResults* object using `args.validation`. This includes helpful methods you can use for displaying error messages.

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


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://cbwire.ortusbooks.com/v2-1/wire-features/validation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
