Form Validation

Last edit:  Mar 27, 2019

Contributors:  kubaugustyn szabosteve Slashek pavelloz lemingos diana-lakatos

Validations are used to ensure that only valid data is saved into your database.
This reference shows you how to validate the state of objects before they go into the database using Form Configuration's validations feature.

After reading this reference, you will know:

  • How to configure form validation
  • How to render validation errors

Example

Simple validation example:

fields:
  properties:
    title:
      validation:
        presence: true

A form with the above configuration will not be valid without title property passed with the request unless it's already set on the object passed to the form.

Validation types

You can use the following validation types:

presence

title:
  validation:
    presence: true

unique

fields:
  properties:
    slug:
      validation:
        presence: true
        unique:
          scope:
            - other_property
          message: Must be unique

confirmation

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

When you set it to true, the system will require a second form value to be sent to validate. The field will be named <field_name>_confirmation.

For example, a password field with confirmation: true will expect both the password and the password_confirmation fields to be equal.

This means you need to add the password_confirmation field to your form configuration. Because it does not have to be saved into the database, it can be virtual.

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

password:
  validation:
    confirmation: true
password_confirmation:
  property_options:
    virtual: true

inclusion / exclusion

   malpractice_history:
      validation:
        inclusion:
          in: [true, false]
          message: disclosure is required

format

Tries to match provided value with regexp defined in with.

When presence is set to false, blank value will produce validation error. To avoid that, use allow_blank: true.

phone:
  validation:
    presence: false
    format:
      with: \A[+]?[\d \-()]+\z
      allow_blank: true
      message: This seems to be an invalid phone number. Please try using numbers only

file_content_type

custom_attachments:
  document:
    file:
      validation:
        presence: true
        file_content_type:
          allow:
          - image/jpeg
          - image/png
          - application/pdf
          - application/msword
          - application/vnd.ms-excel

file_extension

Validation of file extension uploaded as custom_attachment:

Example:

custom_attachments:
  document:
    file:
      validation:
        file_extension:
          allow:
          - .doc
          - .docx

length

attachments:
  validation:
    length:
      is: 2
      message: You need to include Frontside and Backside Photo ID

The possible length constraint options are:

Validation Description Default error message
minimum Cannot have less than the specified number is too short (minimum is %{count} characters)
maximum Cannot have more than the specified number is too long (maximum is %{count} characters)
is 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: 0

Available options are:

Validation Description Default error message
greater_than Input must be greater than the set value must be greater than %{count}
greater_than_or_equal_to Input must be greater than or equal to the set value must be greater than or equal to %{count}
equal_to Input must be equal to the set value must be equal to %{count}
less_than Input must be less than the set value must be less than %{count}
less_than_or_equal_to Input must be less than or equal to the set value must be less than or equal to %{count}
other_than Input must be other than the set value must be other than %{count}
odd Input must be an odd number if set to true must be odd
even Input must be an even number if set to true must be even

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

message

It is possible to overwrite the default message with additional fields:

customizations:
  expertise:
    validation:
      presence:
        message: record is required.

allow_nil

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

allow_blank

Allows passing an empty string.

Validation Errors

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

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


{{ form_object.errors.<field_name> }}

Example:


{{ form_object.errors.name }}

Translation

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

See Translations: System Messages for more information.

Questions?

We are always happy to help with any questions you may have. Check out our Help page, or contact us.