Input validation
Last updated
Last updated
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.
Here, we talk about Text, Text input, Form, and Detail components.
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.
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.
You can configure default validators for Form/Detail components' fields. To do so, follow the instruction below:
Select your component and click on the field you want to add validation to.
Expand the Edit settings section and scroll to the list of validators.
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.
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:
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.
Select the Text input component and navigate to the Validation section.
In the Custom validators dropdown, click Create action.
Select a JavaScript Code action type and specify the following code to modify the validator condition:
Also, you can add a Text component and specify for it the following variables to check input status programmatically:
{{ui.input.valid}}
- indicates whether an input is valid
{{ui.input.validating}}
- indicates if a validator is currently running
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.
Select the Text input component and navigate to the Validation section.
In the Custom validators dropdown, click Create action.
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}}
And for the second step, add a JavaScript Code action type.
Specify the following condition to verify the presence of a user from the API response:
If the user list is not empty, the error message that you specified will be displayed.
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:
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.
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:
Select your Form component and navigate to the Appearance section.
Here, select the Show error message checkbox and add your error message (for example, 'Id field is not valid').
Next, assign a new action for the On Submit trigger.
Select a JavaScript Code action type and specify the following logic to throw a JS error:
Next, click the Submit button to submit the form.