Building a robust content model requires information architects to think not only about the best way of capturing editorial content, but also anticipate human errors that can compromise the model by introducing invalid data.
The easiest way of dealing with content errors is to define specific constraints, expressed in the form of validation rules, and apply them to a given entry field. Once activated, validations will prevent users from storing invalid data and alert them to an identified error.
The guide below introduces the types of validations used in Contentful CMS and provides reference examples of how these rules can be used with common business scenarios.
Contentful stores user input as structured data and, therefore, validations you can apply to a field depend on the type of the field. The table below lists the available validations and indicates with which field types they can be used.
Name | Description | Field type |
---|---|---|
Size | ensures the size of a value is within a specified range | Text, Symbol, Object, Multiple symbols, Multiple entries, Multiple assets |
Predefined values | ensures only predefined values are accepted | Text, Symbol, Number, Decimal number |
Regex | ensures the values matches a regular expression | Text, Symbol |
Range | ensures the value is within a specified range | Integer, Decimal number |
Date range | ensures the date is within a specified range | Date |
Number of items | ensures the number of items is within a specified range | Multiple symbols, Multiple entries, Multiple assets |
Asset file size | ensures the file size is within a specified range | Asset, Multiple assets |
Image dimension | ensures the image dimensions are within a specified range | Asset, Multiple assets |
Type | ensures the asset/reference is of a specified type | Entry, Multiple entries, Asset, Multiple assets |
You can configure validations in a Content Type editor. To add a validation to a field, navigate to the validations dialogue, check the rules you want to invoke and provide the missing values. Validations only take effect after the Content Type has been activated (‘Saved’ for already active content types), thus previously published entries will not be validated until they are published again.
Contentful validations module automatically checks set validations and alerts a user if the specified constraints are impossible to satisfy, for example, because the user has provided incompatible date ranges, entered invalid data or failed to enter a required value.
All validations come with default error messages, but you can also display custom error messages to editors working in Contentful by entering them into an appropriate field under the validation rule.
Validates that the size of a text, object or symbol entry is within a specified range. You can define a range by specifying the ‘min’ and the ‘max’ values (the values are included in the range). Alternatively, you can specify only ‘min’ or ‘max’ value to set a constraint on a the minimum/maximum allowed value.
Validates that the value of a field belongs to a predefined set. To define a set, simply add desired values one by one. Note that currently there is a limit of 50 predefined values you can define. By default, the predefined values will be displayed in the entry editor as a drop-down list.
Validates that the value of a field matches a specific pattern defined by a regular expression. You can further modify pattern matching by including optional flags, such as return all matches (‘g’), perform case-insensitive (‘i’) or multi-line (‘m’) search. Consult JavaScript Regular Expressions for more information on constructing valid regular expressions.
For users’ convenience, Contentful validations module provides a number of predefined regex rules covering mainstream scenarios.
An email consist of a user name, followed by ‘@’ followed by a domain name. Characters allowed in a user name are alphanumeric characters (a-z, 0-9), ‘_’, ‘.’, ‘-’. The same holds for domain names.
Allowed values | Disallowed values |
---|---|
name@domain.com 1-2.3_4@domain.com name@sub.domain.com | special%char@domain.com name@domain notanemail.com |
URL
A valid URL requires a protocol prefix (ftp, http, https) and a top-level domain.
Allowed values | Disallowed values |
---|---|
http://foo.com/blah_blah http://userid:password@example.com:8080 http://foo.com/(something)?after=parens | special%char@domain.com name@domain notanemail.com |
Date (US)
Dates in the format ‘MM/DD/YYYY’ where each character represents a digit. Single-digit months and days may or may not have a leading zero. The year may have two or four digits. Please note that only dates after 1900 are allowed.
Allowed values | Disallowed values |
---|---|
10/11/2012 1-7-1968 5.27.05 | 30 Apr 2001 01-01-1890 21.09.90 |
Date (European)
Dates in the format ‘DD/MM/YYYY’. The same restrictions as for the US dates apply.
Allowed values | Disallowed values |
---|---|
12/12/2014 17-1-1978 27.04.93 15 5 1900 | 12/13/2014 1-17-1978 27041950 12/1998 |
Phone number (US)
Format ‘X-XXX-XXX-XXXX’ or ‘X-(XXX)-XXX-XXXX’ where each X is a digit. Dots (‘.’) and a single whitespace are also accepted as separators instead of ‘-’. The separators may be omitted.
Allowed values | Disallowed values |
---|---|
1-123-456-7890 1-123.555 1111 18001114545 1-(123) 415-1111 | 1_111-111-1111 12345 1-111-1a1-1111 1-800-diamonds |
US zip code
Accepts zip codes entered as five digits (‘XXXXX’) or five plus four digits ‘XXXXX-XXXX’.
Allowed values | Disallowed values |
---|---|
58701-0124 587010124 58701 | 587011 58-7801 58701-0 5870101234 587011-0124 |
Time (12 hrs)
Accepts time values in the HH:MM:SS AM/PM format. Allowed hours are from 01 to 12, columns are required, while the use of seconds is optional. The input must contain AM/PM notation in either lower- or upper-case.
Allowed values | Disallowed values |
---|---|
12:15 am 12.56.01 AM 11 59 03 PM 7:35 PM | 12:15 121503 12am 11:67 |
Time (24 hrs)
Accepts time values in the HH:MM:SS format. Allowed hours are from 01 to 24, columns are required, while the use of seconds is optional. The input cannot contain AM/PM notation.
Allowed values | Disallowed values |
---|---|
12:15 12.56.01 11 59 03 23:57 | 1215 121503 12 am 11:67 |
Validates that the value of a number field is within a specified range. You can use integers or decimal numbers to define the range and negative numbers are also accepted. Alternatively, you can define only the maximum or minimum value.
Validates that the value of a date field is within a specified range. In addition to the date, the range requires time values, set these to 00:00 when the time value is optional.
Please note that for storing time values without a date, we recommend using a relevant Regex rule with a Symbol or Text field type.
Validates that the number of symbols/ entries / assets is within a specified range. Only integers are allowed as input values.
Validates that the size of an asset is within a specified range. Please note that the entered values are stored as bytes, but displayed as Bytes / Kb / Mb.
Validates that the dimensions of an image are within a specified range. The entered values are interpreted as pixels and are inclusive of the range. Alternatively, you can specify only a desired height or width.
Please note that the validation will only be applied to assets that are identified as images.
Validates that the attached entry / uploaded asset is of the specified type. The types of entries are based on content types available within a space. Asset types are based on MIME groups and include:
Attachment Plain text Image Audio Video Rich text | Presentation Spreadsheet PDF document Archive Code Markup |
Please consult Wikipedia page for the list of file types corresponding to specific MIME types.