You are here

function clientside_validation_fapi_regular in Clientside Validation 7

Same name and namespace in other branches
  1. 6 clientside_validation_fapi/clientside_validation_fapi.module \clientside_validation_fapi_regular()
1 call to clientside_validation_fapi_regular()
clientside_validation_fapi_after_build_recurse in clientside_validation_fapi/clientside_validation_fapi.module

File

clientside_validation_fapi/clientside_validation_fapi.module, line 26
Add client side validation support for fapi validation

Code

function clientside_validation_fapi_regular($form_id, $element, &$js_rules) {
  if (isset($element['#name']) && isset($element['#rules'])) {
    $data = _fapi_validation_data('rules');
    foreach ($element['#rules'] as $rule) {
      $params = array(
        $element['#value'],
      );
      $error_message = NULL;

      // If $rule is an array, use error message if is set.
      if (is_array($rule)) {
        if (isset($rule['error'])) {
          $error_message = $rule['error'];
        }
        if (!isset($rule['rule'])) {
          drupal_set_message(t('Rule array with wrong structure on %field.', array(
            '%field' => $element['#name'],
          )), 'error');
          continue;
        }
        $rule = $rule['rule'];
      }
      preg_match('/^(.*?)(\\[(.*)\\])?$/', $rule, $rs);
      $rule = $rs[1];
      if (!isset($data[$rule])) {
        $error_message = t('Rule %rule not found!', array(
          '%rule' => $rule,
        ));
        continue;
      }

      // Parsing parameters.
      if (isset($rs[3]) && $rs[1] != 'regexp') {
        $params[] = preg_split('/ *, */', $rs[3]);
      }
      elseif (isset($rs[3])) {
        $params[] = $rs[3];
      }
      if (count($params) > 1) {
        $params = $params[1];
      }
      $error = is_null($error_message) ? $data[$rule]['error_msg'] : $error_message;
      _clientside_validation_set_fapi_validation($element, $data[$rule], $params, $js_rules, $error);
    }
  }
}