You are here

function _clientside_validation_add_special_rules in Clientside Validation 7

Same name and namespace in other branches
  1. 6 clientside_validation.module \_clientside_validation_add_special_rules()
2 calls to _clientside_validation_add_special_rules()
clientside_validation_form_after_build in ./clientside_validation.module
Regular form.
clientside_validation_webform_after_build in ./clientside_validation.module
Webform.

File

./clientside_validation.module, line 353
Add client side validation to forms.

Code

function _clientside_validation_add_special_rules(&$js_rules, &$settings, $form) {
  $form_id = $form['#id'];
  if (isset($js_rules['_groups'])) {
    $settings['clientsideValidation']['groups'][$form_id] = (object) $js_rules['_groups'];
    unset($js_rules['_groups']);
  }
  else {
    $settings['clientsideValidation']['groups'][$form_id] = new stdClass();
  }
  foreach ($js_rules as $key => $rule) {
    if (isset($rule['checkboxgroupminmax'])) {
      $settings['clientsideValidation']['forms'][$form_id]['checkboxrules'][$key] = array(
        'checkboxgroupminmax' => $rule['checkboxgroupminmax'],
        'messages' => array(
          'checkboxgroupminmax' => $rule['messages']['checkboxgroupminmax'],
        ),
      );
      unset($rule['checkboxgroupminmax']);
      unset($rule['messages']['checkboxgroupminmax']);
      if (empty($rule['messages'])) {
        unset($rule['messages']);
      }
    }
    if (isset($rule['daterange'])) {
      $settings['clientsideValidation']['forms'][$form_id]['daterangerules'][$key] = array(
        'daterange' => $rule['daterange'],
        'messages' => array(
          'daterange' => $rule['messages']['daterange'],
        ),
      );
      unset($rule['daterange']);
      unset($rule['messages']['daterange']);
      if (empty($rule['messages'])) {
        unset($rule['messages']);
      }
    }
    if (isset($rule['datemin'])) {
      $settings['clientsideValidation']['forms'][$form_id]['dateminrules'][$key] = array(
        'daterange' => $rule['datemin'],
        'messages' => array(
          'datemin' => $rule['messages']['datemin'],
        ),
      );
      unset($rule['datemin']);
      unset($rule['messages']['datemin']);
      if (empty($rule['messages'])) {
        unset($rule['messages']);
      }
    }
    if (isset($rule['datemax'])) {
      $settings['clientsideValidation']['forms'][$form_id]['datemaxrules'][$key] = array(
        'daterange' => $rule['datemax'],
        'messages' => array(
          'datemax' => $rule['messages']['datemax'],
        ),
      );
      unset($rule['datemax']);
      unset($rule['messages']['datemax']);
      if (empty($rule['messages'])) {
        unset($rule['messages']);
      }
    }
    if (!empty($rule)) {
      $settings['clientsideValidation']['forms'][$form_id]['rules'][$key] = $rule;
    }
  }

  // Ajax callback: We do not know if only a single field is replaced/added or
  // an entire form, so add the settings twice.
  $matches = array();
  preg_match(' /.*?(--(\\d)*)/', $form_id, $matches);
  if (isset($matches[1])) {
    $form_id = str_replace($matches[1], '', $form_id);
  }
  if ($form_id != $form['#id']) {
    $settings['clientsideValidation']['forms'][$form_id] = $settings['clientsideValidation']['forms'][$form['#id']];
    $settings['clientsideValidation']['groups'][$form_id] = $settings['clientsideValidation']['groups'][$form['#id']];
  }
}