Headless CMS > Essentials
Add Validator to Fields
Learn how to add a validator to a content model's field in Headless CMS.
- what is a field validator
- how to add a validator to a content model’s field
- how to test the applied validator
A field validator helps you ensure the user input meets specific requirements and matches the expected format for a field.
In this tutorial, we will learn how to add validators to a content model’s fields. As an example, we will add validators to the LONG TEXT and NUMBER fields used in the Product content model that we created in the Create Content Model tutorial.
Step 1: AddRequired
andMin length
Validators to a LONG TEXT Field
In this step we will:
- make the Description field required.
- set the minimum length for the input the Description field to 20 characters.
From the Side Menu, Click Headless CMS > Models.
✔️ The Content Models screen opens.
Hover on the Product content model.
Click the EDIT CONTENT MODEL icon (✏️).
✔️ Screen to configure the Product content model’s fields opens.
Click the EDIT FIELD icon (✏️) on the Description field.
✔️ Field Settings - Long Text screen opens.
Click the VALIDATORS tab.
Enable the Required validation by toggling the Enabled button. This will make the Description field mandatory.
✔️ Message textbox will be shown to set the error message.
In the Message textbox, type Please enter the product description.
Enable the Min length validation by toggling the Enabled button.
✔️ Message and Value textboxes will be shown to set the error message and minimum value.
In the Message textbox, type Description cannot be less than 20 characters.
In the Value textbox, type 20.
Click SAVE FIELD.
✔️ Screen to configure the Product content model’s fields opens.
Click SAVE.
✔️ “Your content model was successfully saved!” will be displayed.
Step 2: Test the Validators
Click the PREVIEW tab in the Product Model screen.
Click the Description textbox.
Now click anywhere outside the Description textbox.
✔️ The message “Please enter the product description” appears.
In the Description textbox, type Top sweatshirt.
Now click anywhere outside the Description textbox.
✔️ The message “Description cannot be less than 20 characters” appears.