Validators

Categories

Component ID

2603248

Component name

Validators

Component type

module

Maintenance status

Development status

Component security advisory coverage

covered

Downloads

943

Component created

Component changed

Component body

Introduction

"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.

Requirements

API Hooks

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

Example

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

function mymodule_personal_details_form($form, &$form_state) {

  $form['email_address'] = array(
    '#type' => 'textfield',
    '#title' => t('E-mail address'),
    '#validators' => array(
      'Email'
    ),
  );

  $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' => 'https://en.wikipedia.org/wiki/https://en.wikipedia.org/wiki/International_Bank_Account_Number')
        ),
      )
    ),
  );

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

  return $form;
}