"Validators" allows you to use the Symfony Validator component inside your Drupal projects. The module allows you to extend your forms with an extra parameter called #validators. This validator is designed to validate forms against constraints (i.e. rules). An overview of these constraints can be found on the official component page.


  • PHP Version: 5.3.x or greater.

API Hooks

  1. hook_validator_asserts: Introduce custom validator assertions to the form validator.


In the following example we will validate if the user filled in a valid e-mail address and a valid ISBN number:


namespace Drupal\mymodule\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

 * Contribute form.
class TestForm extends FormBase {
   * {@inheritdoc}
  public function getFormId() {
    return mymodule_example_form';

   * {@inheritdoc}
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['iban'] = array(
      '#type' => 'textfield',
      '#title' => t('Bank account (IBAN format)'),
      '#validators' => array(
        'Iban' => array(
          'message' => t(
            'This value is an invalid bank account number. Please respect the <a href="@url">IBAN format</a>.',
            array('@url' => '')

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),

  return $form;
Supporting organizations: 
sponsored development of this module

Project information