# Input validation

UI Bakery offers various methods of validating user input. You can use our *built-in validators* or create *custom* ones. In this article, we'll explore them in more details.

{% hint style="success" %}
Here, we talk about [Text](https://docs.uibakery.io/reference/working-with-components/text), [Text input](https://docs.uibakery.io/reference/working-with-components/text-input), [Form](https://docs.uibakery.io/reference/working-with-components/form), and [Detail](https://docs.uibakery.io/reference/working-with-components/detail) components.
{% endhint %}

## Default validators

UI Bakery features a range of built-in validators for different inputs, such as *Min/Max*, *Required*, *Regexp*, etc. Validation occurs once a user inputs a value. By default, errors are shown after the input loses focus. But you can change this by clearing the **Show error after touched** checkbox, and then errors will be shown immediately.

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2FbNIijK6HITGa9txhnVnh%2FCleanShot%202024-12-04%20at%2013.20.26%402x-min.png?alt=media&#x26;token=06aa4e9a-4cf8-43af-9ab6-4ff769039235" alt=""><figcaption></figcaption></figure>

You can also create logic based on **validation status**. For this purpose, you can use the `{{ui.input.valid}}` variable that holds a Boolean value indicating the input's validity.

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2FJLpZg6wmvGlwCQtALiRJ%2FCleanShot%202024-12-04%20at%2013.30.53%402x-min.png?alt=media&#x26;token=03d27b3c-d776-4ea2-ae13-21fde4d08241" alt=""><figcaption></figcaption></figure>

### Form/Detail validators

You can configure default validators for *Form/Detail* components' *fields*. To do so, follow the instruction below:

1. Select your component and click on the **field** you want to add validation to.
2. Expand the **Edit** **settings** section and scroll to the list of validators.
3. Set the **Min** setting value as 100, for example, and then input a value less than 100 in the **Id** field.\
   The Form component will react to the input validation, showing an error.

You can adjust this behavior by selecting the **Disable submit when invalid** checkbox which disables submission altogether if the input value is invalid.

{% embed url="<https://demo.arcade.software/4gJadZPdbt6FEIBmNxys?embed=&embed_desktop=inline&embed_mobile=tab&show_copy_link=true>" %}

## Custom validators

Besides using built-in validators, UI Bakery also allows creating custom validators. They are executed when the input changes, displaying an error next to the input.

Custom validators work according to the following **rules**:

* When the input's value changes, the validator is executed
* The `{{params}}` variable holds the current input value, enabling the creation of validation conditions
* If the validator returns a *String* or an *array of Strings*, these will be displayed as errors
* If the validator returns `null` or an *empty* value, validation is considered successful - no errors are shown, and the input is deemed valid
* During validation the input is considered invalid
* Validation is complete once all assigned validators have been executed

If the input value is empty, the validator will display the following error:

```javascript
return {{params}} ? null : 'Field is required';
```

Custom validators can be assigned to different components. Check out the instruction below to learn how to create and assign a custom validator to a *Text input* component. The same way, you can do it for other components as well.

### To create a custom validator:

1. Select the **Text input** component and navigate to the **Validation** section.
2. In the **Custom validators** dropdown, click **Create action.**
3. Select a **JavaScript Code** action type and specify the following code to modify the validator condition:

```javascript
return {{params}} ? null : 'Field is required';
```

4. Also, you can add a **Text** component and specify for it the following variables to check input status programmatically:
   1. &#x20;`{{ui.input.valid}}` - indicates whether an input is valid
   2. `{{ui.input.validating}}` - indicates if a validator is currently running

{% embed url="<https://demo.arcade.software/2Mfw6QRc5QQEHZU1yFE2?embed=&embed_desktop=inline&embed_mobile=tab&show_copy_link=true>" %}

### Async validators

Custom validators can also execute asynchronous validation operations, for example, making an API request to check if an email address is available. Let's review this example in the instruction below.

#### To create an async validator:

1. Select the **Text input** component and navigate to the **Validation** section.
2. In the **Custom validators** dropdown, click **Create action.**
3. For the first step, add an **HTTP Request** action type to verify if a user with the provided email address exists:

`https://example-data.draftbit.com/users?email={{params}}`

4. And for the second step, add a **JavaScript Code** action type.
5. Specify the following condition to verify the presence of a user from the API response:

```javascript
return {{data.length > 0}} ? 'Email is already taken' : null;
```

If the user list is not empty, the error message that you specified will be displayed.

{% embed url="<https://demo.arcade.software/LTfTKYPbCzhZEgitOncg?embed=&embed_desktop=inline&embed_mobile=tab&show_copy_link=true>" %}

### Custom validators for a Form component

You can also assign custom validators to a Form component. These validators receive the entire form object as `{{params}}` input and must return an object where each key is an input name and each value is an error message. This allows the form validator to assign errors to multiple fields in a single run.

To do so, you simply need to create a *JavaScript Code* action with the code below and assign it as a custom validator for the component:

```javascript
return {{params.id}} ? null : { id: 'Field is required', name: 'Field is required' };
```

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2FfqmGxt1dQzzX5Fjvcj4p%2FCleanShot%202024-12-05%20at%2016.24.04%402x-min-min.png?alt=media&#x26;token=02e83a78-0e49-4019-a94f-b71c5b926924" alt=""><figcaption></figcaption></figure>

#### Setting errors manually with `setErrors`

You can also set errors manually for a Form. For example, in some cases, the form should be submitted, but based on the API's error response, it should either succeed or display errors.

For this purpose, you can use the `{{ui.form.setErrors()}}` method to set errors accordingly.

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2F4cfdp7wEiHhjhjUwUEoh%2FCleanShot%202024-12-05%20at%2016.51.52%402x-min-min.png?alt=media&#x26;token=7ba02ad3-5a0a-4077-abb2-053d8ecf29bf" alt=""><figcaption></figcaption></figure>

#### Global error for Form/Detail components

Form and Detail components can also display a **global** error message if an error occurs during the *On Submit* action. Follow the instruction below to enable this functionality:

1. Select your **Form** component and navigate to the **Appearance** section.
2. Here, select the **Show error message** checkbox and add your error message (for example, *'Id field is not valid'*).
3. Next, assign a new action for the **On Submit** trigger.
4. Select a **JavaScript Code** action type and specify the following logic to throw a JS error:

```javascript
if ({{ui.form.value.id}} < 100) {
  throw new Error();
}
```

5. Next, click the **Submit** button to submit the form.

{% embed url="<https://demo.arcade.software/EBQxOIouDP1i4vzeixWs?embed=&embed_desktop=inline&embed_mobile=tab&show_copy_link=true>" %}
