Developers · Forms
Form Validation
Last updated 2 November 2026
Validations help to ensure that only valid data is saved into your database. This guide demonstrates how to validate the state of objects before their database insertion using the validation feature of a form.
After reading this reference, you will know:
- How to configure form validation
- How to render validation errors
Example
Below is a simple validation example:
fields:
properties:
title:
validation:
presence: trueA form is only considered valid if the
Validation Types
You can use the validation types detailed in this section.
presence
title:
validation:
presence: trueunique
fields:
properties:
slug:
validation:
presence: true
unique:
scope:
- other_property
message: Must be uniqueconfirmation
This validation is used when the same value must be entered twice.
Setting it to
As an illustration, when the
This behavior means you must add the
The default error message for this validator is the 'doesn't match %{attribute}'.
password:
validation:
confirmation: true
password_confirmation:
property_options:
virtual: trueChecks if the provided value is a valid email address.
author_email:
validation:
email: trueinclusion/exclusion
malpractice_history:
validation:
inclusion:
in: [true, false]
message: disclosure is requiredformat
Tries to match the provided value with regexp defined in
A blank value will produce a validation error when
phone:
validation:
presence: false
format:
with: A[+]?[d -()]+z
allow_blank: true
message: This is an invalid phone number. Please try using numbers onlylength
attachments:
validation:
length:
is: 2
message: You need to include Frontside and Backside Photo IDThe possible length constraint options are:
| Validation | Description | Default error message |
|---|---|---|
| Cannot have less than the specified number. | is too short (minimum is %{count} characters) | |
| Cannot have more than the specified number. | is too long (maximum is %{count} characters) | |
| The attribute length must be equal to the given number. | is the wrong length (should be %{count} characters) |
numericality
rate_amount:
validation:
presence: true
numericality:
greater_than_or_equal_to: 0Available options are:
| Validation | Description | Default error message |
|---|---|---|
| Input must be greater than the set value. | must be greater than %{count} | |
| Input must be greater than or equal to the set value. | must be greater than or equal to %{count} | |
| Input must be equal to the set value. | must be equal to %{count} | |
| Input must be less than the set value. | must be less than %{count} | |
| Input must be less than or equal to the set value. | must be less than or equal to %{count} | |
| Input must be other than the set value. | must be other than %{count} | |
| Input must be an odd number if set to |
must be odd | |
| Input must be an even number if set to |
must be even |
datetime
Datetime is a date with the exact time down to the second. For example,
The system can convert human phrases (for example,
Note:
my_date:
validation:
datetime:
after: "10 minutes ago"
message: "Must not be earlier than 10 minutes ago."
my_date2:
validation:
datetime:
before: "now"
message: "Must be in the past"The possible date constraint options are:
| Validation | Description |
|---|---|
| Must be at this exact datetime. | |
| Must be earlier than provided datetime. | |
| Must be later than provided datetime. | |
| Must be exactly the datetime or earlier. | |
| Must be exactly the datetime or later. |
All default error messages are in a format
date
my_date:
validation:
date:
after: "1 day from now"
message: "Must be at least one day in the future"The possible date constraint options are:
| Validation | Description |
|---|---|
| Must be at this exact date. | |
| Must be earlier than provided date. | |
| Must be later than the provided date. | |
| Must be exactly the date or earlier. | |
| Must be exactly the date or later. |
All default error messages are in a format
Conditional validation
It is possible to validate attributes based on the state of other attributes from the form.
expiry_year:
validation:
presence:
if: '{% if form.expires %}true{% endif %}'Common validation options
This section details common validation options you can use on your code.
message
It is possible to overwrite the default message with additional fields:
fields:
properties:
expertise:
validation:
presence:
message: Expertise is required.allow_nil
The
allow_blank
Allows passing an empty string.
Validation errors
When a form validation error is raised, an error message is added to the
If you need errors for a particular field, access them through:
{{ form.errors.<field_name> }}Example:
{{ form.errors.name }}Translation
Each error message can be translated with the Insites translation mechanism.
Please visit Translations: System Messages for more information.