The validation settings in the content types builder allow you to make sure that content entered into each field in an entry is fit for purpose.
Content types and entries in Contensis helps save time by allowing you store content in small pieces that you can use anywhere online using the Delivery API. These small pieces of content could appear in many different places – from a website to a wearable device. It’s important that they are formatted correctly, as it might not be possible for editors to check every web page or app that uses the content.
The validation settings in the content types builder allows developers to set criteria that each field in an entry must meet before an editor can publish the entry. Depending on the type of data allowed by the field you are using, you can set whether the content:
When you add a field to a content type you can choose to set no validation or use a combination of the three methods. So, in the case of a text field for a page title, you could stipulate that the field is required, must contain no more than 60 characters, and must be formatted in title case.
Let’s take a look at how you can use the validation settings to make sure your content is fit for purpose and looks great wherever it’s published.
1. Choose a content type and field
Open an existing content type or create a new one. In the content type builder select an existing field or create a new one. Now open the Validation tab in the field settings panel to see the validation settings.
2. Select a validation method
Now choose one of the following validation messages:
This validation method is useful when it’s essential that a field contains a value. When you set a field as required, an editor must enter a value before they can publish the entry. This validation method supports all field types.
To set a field as required change the Required Field toggle to Yes.
The field will now be flagged as required in the entry editor. If an editor tries to publish the entry without completing the field, the entry won’t publish and the field will be highlighted in red.
The Matches pattern method is useful for validating a field where the value should always follow a specific pattern. This includes email addresses, website URLs, and postcodes. This validation method only supports text fields.
To validate a field using the Matches pattern method simply select the expression you would like to use from the dropdown menu.
To add your own regular expression, choose Custom from the Matches pattern dropdown menu and add your regular expression into the empty field.
When an editor enters text that doesn’t exactly match the format you’ve specified, such as an email address, they will see a message telling them that the value they have inputted doesn’t match the expected pattern. In the case of an email address this might be because they have missed out the @ symbol or haven’t added a valid domain extension such as .com or .co.uk. They won’t be able to publish the entry until they have entered a valid email address.
Number of characters
This validation method is especially useful when creating a text field that will be used in a situation where it will be truncated if it’s too long, such as a social media post or meta description. You can specify a minimum number of characters, a maximum number of characters, or a range. This validation method only supports text fields.
To limit a field to a certain number or range of characters choose one of the following options from the Number of characters dropdown:
- Between allows you to set the minimum and maximum values for an expected character range.
- Minimum of allows you to set an expected minimum value that needs to be met.
- Maximum of allows you to set the maximum number of characters that can be entered into a field.
The required number of characters will now be displayed next to the field in the entry editor. The total number of characters will also be shown next to the field as a user types. When a user enters too few or too many characters the whole field will turn red and they will be unable to publish the entry.
3. Customise the validation messages
You can also customise the message that will be displayed when any of these validation methods fail. This message will be displayed if an editor tries to publish the content entry without completing the field. While this isn’t compulsory, it’s good practice, as it creates a better user experience for editors by letting them know what they are doing wrong.
To add a custom validation message simply enter your message into the empty Validation message text box adjacent to the type of validation you are using in the Validation settings tab. Your text will override the standard validation message.
Getting the most out of validation settings
As you can see, setting validation rules is a great way to make sure your content is fit for purpose and isn’t going to break something or look terrible when it is appears on your website, app, or anywhere else you might be using the Delivery API to display content.
When building your content types consider how each field will be used and how you can ensure that the information entered by users is as accurate as possible.
Ask yourself the following questions each time you create a field:
- Is it essential that users complete this field? If leaving the field empty will break something or look bad use the Required rule to ensure that users enter a value.
- Should the value in this field follow a standard pattern like a telephone number or URL? A URL is unusable if a single letter is missing and it’s easy to add an extra digit to a telephone number by mistake. Use the Matches pattern method to validate this kind of content before it’s published.
- Will any limits be placed on the length of this content when it is published on your website or elsewhere? Check how many characters are available and use the Number of characters rule to make sure that the content won’t ruin your design or be abruptly cut off.
Remember – not every user will know where the content they are creating is going to be used and what limitations that places on the content.
For example, while it might be clear that a field will be used for say a meta description, users might not know that descriptions over 160 characters are truncated by search engines.
And, even if your users are familiar with the ins and outs of where the content will be used and how, everybody makes mistakes. It’s far easier to set a character limit and automatically count the characters than have to do it manually.