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: AddRequiredandMin lengthValidators 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 Content Modeling > Models.
✔️ The Content Models screen opens.
Hover over the Product content model.
Click ✏️ Edit.
✔️ 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 Validations tab.
Enable the Required validation by toggling the Enabled button. This will make the Description field mandatory.
✔️ Message textbox will appear 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 appear 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.
✔️ The message “Your content model was saved successfully!” displays.
Step 2: Test the Validators
On the screen to configure the Product content model’s fields, click the Preview tab.
Click the Description textbox.
Click anywhere outside the Description textbox.
✔️ The message “Please enter the product description” appears.
In the Description textbox, type Top sweatshirt.
Click anywhere outside the Description textbox.
✔️ The message “Description cannot be less than 20 characters” appears.