In this article we will discuss the following:
How to add validation rules to a schema field
Different types of validation rules
Before we can continue, you will need a Schema template with a schema field to be able to add validation rules. If you are not sure how to do this, please take a look at the following article: How to create a schema.
How to add validation rules to a schema
Once you've selected a field to add to your schema, by either clicking on the "field" or dragging and dropping the field onto the preview screen, the field will appear on the right-hand side of the screen.
If you click on the field on the right-hand side, the Edit Fields tab will open on the left-hand side. You will see all of the customizable options underneath it.
Scroll to where you can see the Validation Rules section and click on the "plus (+) button" to add a rule.
Select a rule and add a rule option where applicable.
Plus (+) - Add a/another validation rule
Downwards arrow (chevron) - Collapse the panel
A number ("1" in the example below) - The number of the validation rule that is getting added
Delete (X) - Remove the validation rule
Context menu - Add a rule before/after and remove a rule
Rule - Select a rule (will be discussed in the next section)
Rule options - Add a rule option (will be discussed in the next section)
After selecting a rule and adding a rule option, "Save" your changes.
After saving, you can go ahead and add content. Content entries cannot be saved unless all validation rules are met.
Different types of validation rules
We make use of the Validate.js library. For detailed documentation about each validator, please visit the validatejs.org website.
Make sure the input is a valid email.
Rule options:
Message - the message that will be displayed when entering an invalid email
Exclusion
Restrict certain values.
Rule options:
Within - What should be excluded/restricted
Message - the message that will be displayed when entering a restricted value
Pattern (Regex)
Ensure a value conforms/matches a regex pattern.
Rule options:
Pattern - The regex pattern
Flags - Regex flags (example "gi" - global case-insesitive)
Message - the message that will be displayed when entering a restricted value
Inclusion
Ensure the value is one of the specified values provided in the rule.
Rule options:
Within - list of values that should be used to check against the user input
Message - the message that will be displayed when entering a restricted value
Length
Check the length of a string/array.
Rule options:
Is - The value has to have exactly this length
Wrong length message - the message that will be displayed when the length is incorrect
Minimum - The value cannot be shorter than this value
Too short message - the message that will be displayed when the length is too short
Maximum - The value cannot be longer than this value
Too long message - the message that will be displayed when the message is too long
Numerical
Validate the provided user input is a numerical value that satisfies the provided rules.
Rule options:
Only Integer - The value may only be an integer
Strict - The string value will adhere to more strict validations
Greater Than - The value should be greater, and not including the same value
Greater Than or Equal Than - The value should be greater, and including the same value
Equal To - The value should be an exact match
Less Than or Equal To - The value should be less, and including the same value
Less Than - The value should be less, and not including the same value
Divisible By - The value should be divisible by a specific value
Is Odd - The value should be an odd number
Is Even - The value should be an even number
The default messages:
Not Integer Message - must be an integer
Not Valid Message - is not a valid number
Not Greater Than Message - must be greater than %{count}
Not Greater Than or Equal To Message - must be greater than or equal to %{count}
Not Equal To Message - must be equal to %{count}
Not Less Than Message - must be less than %{count}
Not Less Than or Equal To Message - must be less than or equal to %{count}
Not Divisible By Message - must be divisible by %{count}
Not Odd Message - must be odd
Not Even Message - must be even
Required
Ensure a value is provided by the user.
Rule options:
Message - There should be a message to indicate that it is a required field
Allow Empty - Toggle the switch on to indicate to the user that it may be left empty to pass the validation
URL
Ensure the value provided conforms to a valid URL pattern.
Rule options:
Message - If the URL is not valid a message should display for the user
Schemes - There can be a list of approved schemes. The included default items for Flamelink are, 'http' and 'https'.
Allow Local - If the boolean value amounts to true (toggle the switch on), the approved local host names can be used
Remember to select the "Save" button, to not loose the rules you have created. Below the rules you will see the Conditions to add where applicable.
The Condition fields to specify are the Property, Operator, and Value. For the Condition fields, it will only be shown in the content editor if all the conditions are met.
To read more about other customizable options for a schema field, check out this article: