Form Validation

Last updated on June 26, 2024.

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:

A form is only considered valid if the property is included in the request unless the property has already been set in the object passed to the form.

Validation Types

You can use the validation types detailed in this section.

presence

unique

confirmation

This validation is used when the same value must be entered twice.

Setting it to makes the system mandate the transmission of a second form value for validation, designated as .

As an illustration, when the attribute is set for a password field, the values entered in both the and fields must match.

This behaviour means you must add the field to your form configuration. Because it does not have to be saved into the database, it can be set as a virtual field.

The default error message for this validator is the 'doesn't match %{attribute}'.

email

Checks if the provided value is a valid email address.

inclusion/exclusion

format

Tries to match the provided value with regexp defined in .

A blank value will produce a validation error when is set to . To avoid that, use .

length

The 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

Available 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, , , , ) to datetime so that you can write them easier.

Note: will take yesterday's date at midnight; if you want to preserve the time of the day, use

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 where the first is the validation name (is_at, before etc.), and the second is the argument of this validation. Suppose the provided argument is not a valid datetime (for example, ). A 501 error is raised in that case, and a Log is created with a Date/Time Validation Error description.

date

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 where the first is the validation name (is_at, before etc.), and the second is the argument of this validation. If the provided argument is not a valid date (for example, ), a 501 error is raised, and a Log is created with the Date/Time Validation Error description.

Conditional validation

It is possible to validate attributes based on the state of other attributes from the form.

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:

allow_nil

The option skips the validation when the value being validated is nil.

allow_blank

Allows passing an empty string.

Validation errors

When a form validation error is raised, an error message is added to the object that contains errors corresponding to fields.

If you need errors for a particular field, access them through:

Example:

Translation

Each error message can be translated with the Insites translation mechanism.

Please visit Translations: System Messages for more information.

Have a suggestion for this page?

Didn't quite find what you are looking for or have feedback on how we can make the content better then we would love to hear from you. Please provide us feedback and we will get back to you shortly.