Validation

Add server-side validation for your form. This plugin allows you to configure validation rules for each of your form inputs to ensure the data being submitted meets your expectations.

By default, when a form is submitted and fails validation, the user will be redirected to Postform and shown a list of validation errors:

Form Submission Errors

If you're using the Submit Redirect plugin or submitting the form using AJAX, you will have to implement error handling to inform the user that an error has occurred.

Any form submissions that fail validation can be found in your form's spam submission list.

Plugin configuration

Click on the "Add Field Validation" button to configure validation for a form input:

Configure Plugin

This will open up the following panel:

Configure Field Validation

And you'll see the following fields:

  • Input Field Name - the "name" attribute of your HTML form input. For example, if your HTML form input is <input name="note" />, you would enter note as the input field name.
  • Public Field Label - human readable label to use in error messages
  • Data Type - the expected data type (text, number, date, etc.) for the input
  • Required - check if the field is mandatory

Data Types

Depending on the "Data Type" you select, you will be presented with additional configuration options.

Date

For the "Date" data type, you can configure the minimum and maximum date allowed for the field. In addition to any date value, you can enter now to always use the current date.

Configure Date Field Validation

Number

For the "Number" data type, you can configure the following:

  • Min - the minimum number allowed
  • Max - the maximum number allowed
  • Valid Values - a complete list of possible values allowed

Configure Number Field Validation

Text

For the "Text" data type, you can configure the following:

  • Subtype - supports the following options:
    • Email - requires value to be a valid email address
    • URL - requires value to be a valid URL
  • Regular Expression Pattern - a pattern the value must match, for example, use ^[a-zA-Z0-9]+$ to only allow alphanumeric characters
  • Min. Length - the minimum number of characters allowed
  • Max. Length - the maximum number of characters allowed
  • Valid Values - a complete list of possible values allowed

Configure Text Field Validation

Error handling

AJAX

When submitting the form using AJAX and a validation error occurs, a 422 response code will be returned along with a response in the following format:

{
  "errors": [
    {
      "message": "\"Name\" is required",
      "path": [
        "test"
      ],
      "type": "any.required",
      "context": {
        "label": "Name",
        "key": "test"
      }
    },
    {
      "message": "\"Email\" must be a valid email",
      "path": [
        "email"
      ],
      "type": "string.email",
      "context": {
        "value": "fsdafds",
        "invalids": [
          "fsdafds"
        ],
        "label": "Email",
        "key": "email"
      }
    },
    {
      "message": "\"Rating\" must be less than or equal to 5",
      "path": [
        "rating"
      ],
      "type": "number.max",
      "context": {
        "limit": 5,
        "value": 10,
        "label": "Rating",
        "key": "rating"
      }
    }
  ]
}

Submit Redirect plugin

If your form is also using the Submit Redirect plugin, validation errors will be passed as query string parameters to your redirect URL.

If an error has occurred, the validation[error] query parameter will exist and have a value of true.

Each error will be described by the following set of query parameters where i is the index (starting at 0) of the error:

  • validation[i][message] - human readable message that can be displayed in the UI
  • validation[i][path] - the name of the input
  • validation[i][type] - error code describing the type of error that occurred

Here's an example URL:

https://example.com?validation%5Berror%5D=true&validation%5B0%5D%5Bmessage%5D=%22Name%22%20is%20required&validation%5B0%5D%5Bpath%5D=test&validation%5B0%5D%5Btype%5D=any.required&validation%5B1%5D%5Bmessage%5D=%22Email%22%20must%20be%20a%20valid%20email&validation%5B1%5D%5Bpath%5D=email&validation%5B1%5D%5Btype%5D=string.email&validation%5B2%5D%5Bmessage%5D=%22Rating%22%20must%20be%20less%20than%20or%20equal%20to%205&validation%5B2%5D%5Bpath%5D=rating&validation%5B2%5D%5Btype%5D=number.max
Was this page helpful?