You are here

regex_field_validation.module in RegEx Field Validation 7

RegEx Field Validation module file.

File

regex_field_validation.module
View source
<?php

/**
 * @file
 * RegEx Field Validation module file.
 */

/**
 * Implements hook_form_field_ui_field_edit_form_alter().
 */
function regex_field_validation_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  $field_types = array(
    'text_textarea',
    'text_textfield',
  );
  $field_instance = field_info_instance($form['instance']['entity_type']['#value'], $form['#field']['field_name'], $form['instance']['bundle']['#value']);
  if (in_array($field_instance['widget']['type'], $field_types) && $field_instance['settings']['text_processing'] == 0) {
    $settings = _regex_field_validation_get_instance_defaults($field_instance['settings']);
    $field_state = array(
      'visible' => array(
        ':input[name$="[regex_field_validation_widget][enable]"]' => array(
          'checked' => TRUE,
        ),
      ),
    );
    $form['instance']['settings']['regex_field_validation_widget'] = array(
      '#type' => 'fieldset',
      '#title' => t('RegEx Field Validation'),
      '#collapsible' => TRUE,
      '#collapsed' => !$field_state,
    );
    $form['instance']['settings']['regex_field_validation_widget']['enable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Validate field with RegEx'),
      '#description' => t('Validate field content using regular expression.'),
      '#default_value' => $settings['enable'],
    );
    $form['instance']['settings']['regex_field_validation_widget']['regex'] = array(
      '#type' => 'textarea',
      '#description' => t('Regular Expression'),
      '#default_value' => '',
      '#states' => $field_state,
      '#default_value' => $settings['regex'],
    );
    $form['instance']['settings']['regex_field_validation_widget']['error_message'] = array(
      '#type' => 'textfield',
      '#title' => t('Error message'),
      '#default_value' => '',
      '#description' => t('The error message that will be displayed if the field does not validate'),
      '#states' => $field_state,
      '#default_value' => $settings['error_message'],
    );
  }
}

/**
 * Returns default values for the configuration form.
 */
function _regex_field_validation_get_instance_defaults($field_instance) {
  $settings = array(
    'enable' => FALSE,
    'regex' => NULL,
    'error_message' => NULL,
  );
  if ($field_instance != NULL) {
    if (isset($field_instance['regex_field_validation_widget'])) {
      if (isset($field_instance['regex_field_validation_widget']['enable'])) {
        if ($field_instance['regex_field_validation_widget']['enable'] == 1) {
          $settings['enable'] = TRUE;
        }
        else {
          $settings['enable'] = FALSE;
        }
      }
      if (isset($field_instance['regex_field_validation_widget']['regex'])) {
        $settings['regex'] = $field_instance['regex_field_validation_widget']['regex'];
      }
      if (isset($field_instance['regex_field_validation_widget']['error_message'])) {
        $settings['error_message'] = $field_instance['regex_field_validation_widget']['error_message'];
      }
    }
    else {
      return $settings;
    }
    return $settings;
  }
  else {
    return $settings;
  }
}

/**
 * Implements hook_form_alter().
 */
function regex_field_validation_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['#node_edit_form'])) {
    if ($form['#node_edit_form'] == TRUE) {
      foreach ($form_state['field'] as $field) {
        if (isset($field['und']['instance']['settings']['regex_field_validation_widget']['enable'])) {
          if ($field['und']['instance']['settings']['regex_field_validation_widget']['enable'] == 1) {
            $form[$field['und']['field']['field_name']]['#element_validate'] = array(
              '_regex_field_validation_regex_validate',
            );
          }
        }
      }
    }
  }
}

/**
 * Validates field value against regular expression.
 */
function _regex_field_validation_regex_validate($element, $form_state) {
  if (!preg_match_all($form_state['field'][$element['und']['#field_name']]['und']['instance']['settings']['regex_field_validation_widget']['regex'], $form_state['values'][$element['und']['#field_name']]['und'][0]['value'])) {
    form_set_error($element['und']['#field_name'], t($form_state['field'][$element['und']['#field_name']]['und']['instance']['settings']['regex_field_validation_widget']['error_message']));
  }
}

/**
 * Implements hook_help().
 */
function regex_field_validation_help($path, $arg) {
  if ($path == 'admin/help#regex_field_validation') {
    $output = '';
    $output .= '<h3>' . t('About') . '</h3>';
    $output .= '<p>' . t('This module provides an extra validation option for text fields') . '</p>';
    $output .= '<p>' . t('It uses the power of regular expressions to validate the content from fields') . '</p>';
    $output .= '<h4>' . t('How to use it?') . '</h4>';
    $output .= '<ul>';
    $output .= '<li>' . t('Go to Manage Fields on any Content type') . '</li>';
    $output .= '<li>' . t('Edit any text field settings') . '</li>';
    $output .= '<li>' . t('Locate the RegEx Field Validation box that should be available in the settings form') . '</li>';
    $output .= '<li>' . t('Check the Validate field with RegEx option to activate the Regular Expression and Error message fields') . '</li>';
    $output .= '<li>' . t('Type in the regular expression that you want the field value to be matched against') . '</li>';
    $output .= '<li>' . t('Type in the error message that you want to be displayed when the field value does not validate') . '</li>';
    $output .= '</ul>';
    $output .= '<h4>' . t('Examples of regular expressions that can be used') . '</h4>';
    $output .= '<ul>';
    $output .= '<li>' . '<code>' . '\\^\\d.\\d+\\Z\\' . '</code >&nbsp;-&nbsp;' . '<b>' . t('Decimal followed by dot and unlimited decimals (1.12, 1.55, etc.') . '</b></li>';
    $output .= '<li>' . '<code>' . '\\^[^<\\x09]{0,100}\\Z\\' . '</code>&nbsp;-&nbsp;' . '<b>' . t('Text containing between 0 and 100 characters.') . '</b></li>';
    $output .= '<li>' . '<code>' . '\\^[AaBb]\\Z\\' . '</code >&nbsp;-&nbsp;' . '<b>' . t('1 character either A, a, B or b.') . '</b></li>';
    $output .= '<li>' . '<code>' . '\\^(http|https):\\/\\/.{2,80}\\Z\\' . '</code>&nbsp;-&nbsp;' . '<b>' . t('URL starting with “http” or “https” and contains between 2 and 80 characters.') . '</b></li>';
    $output .= '<li>' . '<code>' . '\\^.{2,40}\\@.{2,50}\\..{2,5}\\Z\\' . '</code >&nbsp;-&nbsp;' . '<b>' . t('Email address containing between 2 and 40 characters before the “@”, then between 2 and 50 characters as the domain name and between 2 and 5 as the top level domain') . '</b></li>';
    $output .= '<li>' . '<code>' . '\\^(0[1-9]|1[0-2])\\Z\\' . '</code>&nbsp;-&nbsp;' . '<b>' . t('2 digits that could represent the day of the month (01-31)') . '</b></li>';
    $output .= '<li>' . '<code>' . '\\^(19|20|21)[0-9]{2}\\Z\\' . '</code >&nbsp;-&nbsp;' . '<b>' . t('4 digits that could represent the year (1900-2199)') . '</b></li>';
    $output .= '<li>' . '<code>' . '\\^(ABC|DEF|GHI|JKL|MNO|PQR|STU|VWX)?\\Z\\' . '</code>&nbsp;-&nbsp;' . '<b>' . t('Accept a tree letter string that can be found in that list') . '</b></li>';
    $output .= '<li>' . '<code>' . '\\^([0-9]+(\\.[0-9]{2})?)?\\Z\\' . '</code >&nbsp;-&nbsp;' . '<b>' . t('Numeric with "." as decimal separator (29.99)') . '</b></li>';
    $output .= '<li>' . '<code>' . '\\^[0-9.]{1,8}\\Z<\\' . '</code>&nbsp;-&nbsp;' . '<b>' . t('Numerical value between 1 and 8 digits.') . '</b></li>';
    $output .= '<li>' . '<code>' . '\\^[^<\\x09\\x0a\\x0d]{0,10}\\Z\\' . '</code >&nbsp;-&nbsp;' . '<b>' . t('Single line between 0 and 10 characters that should not contain HTML markup') . '</b></li>';
    $output .= '<li>' . '<code>' . '\\^[^<]{0,100}\\Z\\' . '</code>&nbsp;-&nbsp;' . '<b>' . t('Multiple lines between 0 and 100 characters that should not contain HTML markup') . '</b></li>';
    $output .= '<li>' . '<code>' . '\\^[^<\\x09\\x0a\\x0d]{0,1000}\\Z\\' . '</code >&nbsp;-&nbsp;' . '<b>' . t('Text containing between 0 and 1000 letters, numbers and spaces') . '</b></li>';
    $output .= '</ul>';
    return $output;
  }
}

Functions

Namesort descending Description
regex_field_validation_form_alter Implements hook_form_alter().
regex_field_validation_form_field_ui_field_edit_form_alter Implements hook_form_field_ui_field_edit_form_alter().
regex_field_validation_help Implements hook_help().
_regex_field_validation_get_instance_defaults Returns default values for the configuration form.
_regex_field_validation_regex_validate Validates field value against regular expression.