You are here

function webform_validation_webform_validation_validators in Webform Validation 7

Same name and namespace in other branches
  1. 6 webform_validation.validators.inc \webform_validation_webform_validation_validators()

Implements hook_webform_validation_validators().

This function returns an array of validators, in the validator key => options array form. Possible options:

  • name (required): name of the validator.
  • component_types (required): defines which component types can be validated by this validator. Specify 'all' to allow all types.
  • negatable (optional): define whether the rule can be negated, meaning it will validate the inverse of the rule.
  • custom_error (optional): define whether a user can specify a custom error message upon creating the validation rule.
  • custom_data (optional): define whether custom data can be added to the validation rule.
  • min_components (optional): define the minimum number of components to be selected for creating a validation rule.
  • max_components (optional): define the maximum number of components to be selected for creating a validation rule.
  • description (optional): provide a descriptive explanation about the validator.

File

./webform_validation.validators.inc, line 29
Provides validation functionality and hooks.

Code

function webform_validation_webform_validation_validators() {
  $validators = array(
    'numeric' => array(
      'name' => t('Numeric values'),
      'component_types' => array(
        'hidden',
        'number',
        'textfield',
      ),
      'custom_data' => array(
        'label' => t('Numeric validation range'),
        'description' => t('Optionally specify the minimum-maximum range to validate the user-entered numeric value against. Usage:') . theme('table', array(
          'header' => array(
            t('Value'),
            t('Meaning'),
          ),
          'rows' => array(
            array(
              t('(empty)'),
              t('No value validation'),
            ),
            array(
              '100',
              t('Greater than or equal to 100'),
            ),
            array(
              '|100',
              t('Less than or equal to 100 (including negative numbers)'),
            ),
            array(
              '0|100',
              t('Greater than or equal to 0 & less than or equal to 100'),
            ),
            array(
              '10|100',
              t('Greater than or equal to 10 & less than or equal to 100'),
            ),
            array(
              '-100|-10',
              t('Greater than or equal to -100 & less than or equal to -10'),
            ),
          ),
        )),
        'validate_regex' => '/^((-?[\\d.]+)?\\|)?(-?[\\d.]+)$/',
        'required' => FALSE,
      ),
      'description' => t('Verifies that user-entered values are numeric, with the option to specify min and / or max values.'),
    ),
    'min_length' => array(
      'name' => t('Minimum length'),
      'component_types' => array(
        'email',
        'hidden',
        'number',
        'textarea',
        'textfield',
      ),
      'custom_data' => array(
        'label' => t('Minimum number of characters'),
        'description' => t('Specify the minimum number of characters that have to be entered to pass validation.'),
        'validate_regex' => '/^\\d+$/',
      ),
      'description' => t('Verifies that a user-entered value contains at least the specified number of characters.'),
    ),
    'max_length' => array(
      'name' => t('Maximum length'),
      'component_types' => array(
        'email',
        'hidden',
        'number',
        'textarea',
        'textfield',
      ),
      'custom_data' => array(
        'label' => t('Maximum number of characters'),
        'description' => t('Specify the maximum number of characters that can be entered to pass validation.'),
        'validate_regex' => '/^\\d+$/',
      ),
      'description' => t('Verifies that a user-entered value contains at most the specified number of characters.'),
    ),
    'min_words' => array(
      'name' => t('Minimum number of words'),
      'component_types' => array(
        'hidden',
        'html_textarea',
        'textarea',
        'textfield',
      ),
      'custom_data' => array(
        'label' => t('Minimum number of words'),
        'description' => t('Specify the minimum number of words that have to be entered to pass validation. Words are defined as strings of letters separated by spaces.'),
        'validate_regex' => '/^\\d+$/',
      ),
      'description' => t('Verifies that a user-entered value contains at least the specified number of words.'),
    ),
    'max_words' => array(
      'name' => t('Maximum number of words'),
      'component_types' => array(
        'hidden',
        'html_textarea',
        'textarea',
        'textfield',
      ),
      'custom_data' => array(
        'label' => t('Maximum number of words'),
        'description' => t('Specify the maximum number of words that have to be entered to pass validation. Words are defined as strings of letters separated by spaces.'),
        'validate_regex' => '/^\\d+$/',
      ),
      'description' => t('Verifies that a user-entered value contains at most the specified number of words.'),
    ),
    // Only available in Webform 4; removed below if not.
    'sum' => array(
      'name' => t('Adds up to'),
      'component_types' => array(
        'number',
      ),
      'custom_data' => array(
        'label' => t('Number the fields add up to'),
        'description' => t('Specify the number and the type of comparison. For example:') . theme('table', array(
          'header' => array(
            t('Value'),
            t('Meaning'),
          ),
          'rows' => array(
            array(
              '=3',
              t('The components must add up to exactly 3.'),
            ),
            array(
              '>10',
              t('The components must add up to greater than 10.'),
            ),
            array(
              '>=10',
              t('The components must add up to greater than or equal to 10.'),
            ),
            array(
              '<20',
              t('The components must add up to less than 20.'),
            ),
            array(
              '<=20',
              t('The components must add up to less than or equal to 20.'),
            ),
          ),
        )),
        'validate_regex' => '/^(=|>|>=|<|<=)(-?[\\d.]+)$/',
      ),
      'description' => t('Require the values of the selected fields to add up to exactly, greater than or equal to, or less than or equal to a specified number.'),
    ),
    'equal' => array(
      'name' => t('Equal values'),
      'component_types' => array(
        'date',
        'email',
        'hidden',
        'number',
        'select',
        'textarea',
        'textfield',
        'time',
        'boolean',
      ),
      'min_components' => 2,
      'description' => t('Verifies that all specified components contain equal values. If all components are of type email, they will get case-insensitive comparison.'),
    ),
    'comparison' => array(
      'name' => t('Compare two values'),
      'component_types' => array(
        'date',
        'email',
        'hidden',
        'number',
        'select',
        'textarea',
        'textfield',
        'time',
      ),
      'custom_error' => TRUE,
      'custom_data' => array(
        'label' => t('Comparison operator'),
        'description' => t('Specify the comparison operator you want to use. Must be one of: >, >=, <, <=. The validator will compare the first component with the second using this operator. If the components are of type email, they will get case-insensitive comparison.'),
        'validate_regex' => '/^(>|>=|<|<=)$/',
      ),
      'min_components' => 2,
      'max_components' => 2,
      'description' => t('Compare two values for greater than (>), less than (<), greater than or equal to (>=), or less than or equal to (<=).'),
    ),
    'unique' => array(
      'name' => t('Unique values'),
      'component_types' => array(
        'date',
        'email',
        'hidden',
        'number',
        'select',
        'textarea',
        'textfield',
        'time',
        'boolean',
      ),
      'min_components' => 2,
      'description' => t('Verifies that none of the specified components contain the same value as another selected component in this submission. (To check that values are unique between submissions, use the unique validation option on the "Edit component" page for that component.) If all components are of type email, they will get case-insensitive comparison.'),
    ),
    'specific_value' => array(
      'name' => t('Specific value(s)'),
      'negatable' => TRUE,
      'component_types' => array(
        'email',
        'hidden',
        'number',
        'select',
        'textarea',
        'textfield',
        'boolean',
      ),
      'custom_error' => TRUE,
      'custom_data' => array(
        'label' => t('(Key) value'),
        'description' => t('Specify the specific value(s) you want the component to contain. Separate multiple options by a comma. For components that have keys, use the key value instead.'),
      ),
      'description' => t('Verifies that the value of the specified component is from a list of allowed values.'),
    ),
    'default_value' => array(
      'name' => t('Default value'),
      'negatable' => TRUE,
      'component_types' => array(
        'email',
        'hidden',
        'number',
        'select',
        'textarea',
        'textfield',
        'boolean',
      ),
      'custom_error' => TRUE,
      'description' => t('Verifies that the user-entered value is the default value for that component. Negate if you want not the default value.'),
    ),
    'someofseveral' => array(
      'name' => t('Some of several'),
      'component_types' => array(
        'date',
        'email',
        'file',
        'number',
        'select',
        'textarea',
        'textfield',
        'time',
        'boolean',
      ),
      'custom_data' => array(
        'label' => t('Number to be completed'),
        'description' => t('Specify the number that must be completed and the type of comparison. For example:') . theme('table', array(
          'header' => array(
            t('Value'),
            t('Meaning'),
          ),
          'rows' => array(
            array(
              '>=1',
              t('The user must complete <b>at least</b> 1 of the selected components.'),
            ),
            array(
              '=3',
              t('The user must complete <b>exactly</b> 3 of the selected components.'),
            ),
            array(
              '<=2',
              t('The user must complete <b>at most</b> 2 of the selected components.'),
            ),
          ),
        )),
        'validate_regex' => '/^[><]?=\\d+$/',
      ),
      'min_components' => 2,
      'description' => t('Requires the user to complete some number of components out of a group of components. For example, complete at least 2 out of 3, complete at most 4 out of 6, or complete exactly 3 our of 4.'),
    ),
    'select_min' => array(
      'name' => t('Minimum number of selections required'),
      'component_types' => array(
        'select',
      ),
      'custom_data' => array(
        'label' => t('Minimum number of selections'),
        'description' => t('Specify the minimum number of options a user should select.'),
        'validate_regex' => '/^\\d+$/',
      ),
      'description' => t('Forces the user to select at least a defined number of options from the specified webform components.'),
    ),
    'select_max' => array(
      'name' => t('Maximum number of selections allowed'),
      'component_types' => array(
        'select',
      ),
      'custom_data' => array(
        'label' => t('Maximum number of selections'),
        'description' => t('Specify the maximum number of options a user can select.'),
        'validate_regex' => '/^\\d+$/',
      ),
      'description' => t('Forces the user to select at most a defined number of options from the specified webform components.'),
    ),
    'select_exact' => array(
      'name' => t('Exact number of selections required'),
      'negatable' => TRUE,
      'component_types' => array(
        'select',
      ),
      'custom_data' => array(
        'label' => t('Number of selections'),
        'description' => t('Specify how many options a user can select.'),
        'validate_regex' => '/^\\d+$/',
      ),
      'description' => t('Forces the user to select exactly the defined number of options from the specified webform components.'),
    ),
    'plain_text' => array(
      'name' => t('Plain text (disallow tags)'),
      'negatable' => TRUE,
      'component_types' => array(
        'email',
        'hidden',
        'textarea',
        'textfield',
      ),
      'description' => t("Verifies that user-entered data doesn't contain HTML tags."),
    ),
    'starts_with' => array(
      'name' => t('Starts with'),
      'negatable' => TRUE,
      'component_types' => array(
        'email',
        'hidden',
        'number',
        'textarea',
        'textfield',
      ),
      'custom_data' => array(
        'label' => t('Starts with'),
        'description' => t('Enter the text that this field must start with.'),
      ),
      'description' => t('Verifies that user-entered data starts with a given string.'),
    ),
    'ends_with' => array(
      'name' => t('Ends with'),
      'negatable' => TRUE,
      'component_types' => array(
        'email',
        'hidden',
        'number',
        'textarea',
        'textfield',
      ),
      'custom_data' => array(
        'label' => t('Ends with'),
        'description' => t('Enter the text that this field must end with.'),
      ),
      'description' => t('Verifies that user-entered data ends with a given string.'),
    ),
    'pattern' => array(
      'name' => t('Pattern'),
      'component_types' => array(
        'hidden',
        'textarea',
        'textfield',
      ),
      'custom_error' => TRUE,
      'custom_data' => array(
        'label' => t('Pattern'),
        'description' => t('Specify a pattern where:') . theme('table', array(
          'header' => array(
            t('Value'),
            t('Meaning'),
          ),
          'rows' => array(
            array(
              '@',
              t('Any letter A-Z.'),
            ),
            array(
              '#',
              t('Any numeral 0-9.'),
            ),
            array(
              '|',
              t('Separates two or more acceptable patterns.'),
            ),
            array(
              '',
              t('Any other character must appear in its exact position.'),
            ),
          ),
        )) . t('Examples:') . theme('table', array(
          'header' => array(
            t('Value'),
            t('Meaning'),
          ),
          'rows' => array(
            array(
              '(###) ###-####',
              t('North American phone number.'),
            ),
            array(
              'D-25##',
              t('D-2500 series model numbers.'),
            ),
            array(
              '@# #@@|@## #@@|@@# #@@|@@## #@@|@#@ #@@|@@#@ #@@|GIR0AA',
              t('UK Postal Code.'),
            ),
          ),
        )),
      ),
      'description' => t('Verifies that a user-entered value follows to a specified pattern.'),
    ),
    'regex' => array(
      'name' => t('Regular expression, case-sensitive'),
      'negatable' => TRUE,
      'component_types' => array(
        'email',
        'hidden',
        'number',
        'textarea',
        'textfield',
      ),
      'custom_error' => TRUE,
      'custom_data' => array(
        'label' => t('Regex code (case-sensitive)'),
        'description' => t('Specify regex code to validate the user input against. Do not include delimiters such as /.'),
      ),
      'description' => t('Validates user-entered text against a case-sensitive regular expression.'),
    ),
    'regexi' => array(
      'name' => t('Regular expression, case-insensitive'),
      'negatable' => TRUE,
      'component_types' => array(
        'email',
        'hidden',
        'number',
        'textarea',
        'textfield',
      ),
      'custom_error' => TRUE,
      'custom_data' => array(
        'label' => t('Regex code (case-insensitive)'),
        'description' => t('Specify regex code to validate the user input against. Do not include delimiters such as /.'),
      ),
      'description' => t('Validates user-entered text against a case-insensitive regular expression.'),
    ),
    'must_be_empty' => array(
      'name' => t('Must be empty'),
      'component_types' => array(
        'hidden',
        'number',
        'textarea',
        'textfield',
      ),
      'description' => t('Verifies that a specified textfield remains empty. Recommended use case: used as an anti-spam measure by hiding the element with CSS.'),
    ),
    'blacklist' => array(
      'name' => t('Words blacklist'),
      'negatable' => TRUE,
      'component_types' => array(
        'email',
        'hidden',
        'textarea',
        'textfield',
      ),
      'custom_error' => TRUE,
      'custom_data' => array(
        'label' => t('Blacklisted words'),
        'description' => t('Specify illegal words, seperated by commas.'),
      ),
      'description' => t("Validates that user-entered data doesn't contain any of the specified illegal words."),
    ),
    'username' => array(
      'name' => t('Must match a username'),
      'negatable' => TRUE,
      'component_types' => array(
        'hidden',
        'textfield',
      ),
      'description' => t('Validates that user-entered data matches a username.'),
    ),
    'valid_url' => array(
      'name' => t('Valid URL'),
      'negatable' => TRUE,
      'component_types' => array(
        'hidden',
        'textfield',
      ),
      'description' => t('Validates that the user-entered data is a valid URL.'),
    ),
  );

  // Only available in Webform 4.
  module_load_include('inc', 'webform', 'components/number');
  if (!function_exists('webform_compare_floats')) {
    unset($validators['sum']);
  }
  if (module_exists('email_verify')) {
    $validators['email_verify'] = array(
      'name' => t('Email Verify'),
      'component_types' => array(
        'email',
      ),
      'description' => t('Verifies that an email address actually exists using the <a href="https://drupal.org/project/email_verify">Email Verification module</a>.'),
    );
  }
  if (module_exists('maxlength')) {
    $validators['max_length']['description'] .= ' ' . t('A JavaScript counter will show the number of characters remaining as the user types.');
  }
  return $validators;
}