You are here

function fapi_validation_validators_execute in Form API Validation 8

Function for executing all validators

1 call to fapi_validation_validators_execute()
fapi_validate_element_validate in ./fapi_validation.module
Run element validation callbacks.

File

./fapi_validation.module, line 91
Form API validation module

Code

function fapi_validation_validators_execute(&$element, FormStateInterface $form_state) {

  // If element is empty and not required, by pass rule validation
  if (!$element['#required'] && empty($element['#value'])) {
    return;
  }

  //establish errors array
  $errors = array();

  // Load Validators.
  $validators = _fapi_validation_data('validators');

  // Loop through all validators on the field
  foreach ($element['#validators'] as $validator) {

    // Set error message and error callback all to NULL.
    $error_message = $error_callback = NULL;

    // Some Validators can be an array.
    if (is_array($validator)) {
      if (!isset($validators['validator'])) {
        drupal_set_message(t('Validator array with wrong structure on %field.', array(
          '%field' => $element['#name'],
        )), 'error');
        continue;
      }
      if (isset($validator['error'])) {
        $error_message = $validator['error'];
      }
      if (isset($validator['error_callback'])) {
        $error_callback = $validator['error_callback'];
      }
      $validator = $validator['validator'];
    }

    // Break rule into various aspects, incase it's all passed as one string?
    preg_match('/^(.*?)(\\[(.*)\\])?$/', $validator, $rs);
    $validator = $rs[1];

    // Check if rule exists
    if (!isset($validators[$validator])) {
      drupal_set_message(t('Validator %validator not found!', array(
        '%validator' => $validator,
      )), 'error');
      continue;
    }

    //Get element value
    $params = array(
      $element['#value'],
    );

    // Parsing parameters from validator input
    // @TODO: Review that this is the best way to do this... I'd prefer this was passed as an array value.
    if (isset($rs[3])) {
      if ($validator == 'regexp') {
        $params[] = array(
          $rs[3],
        );
      }
      else {
        $params[] = preg_split('/ *, */', $rs[3]);
      }
    }

    // Call the validator function, pass it the $element and $form_state values.
    $validator_success = call_user_func_array($validators[$validator]['callback'], array(
      $element['#value'],
      $form_state,
    ));
    if (!$validator_success) {
      if (!is_null($error_callback)) {
        $error_params = array(
          $validator,
          $params,
          $element,
          $form_state,
        );
        $errors[] = call_user_func_array($error_callback, $error_params);
      }
      else {
        $error = is_null($error_message) ? $validators[$validator]['error_msg'] : $error_message;
        $error = str_replace('%field', $element['#title'], $error
          ->render());
        $errors[] = t($error, array(
          '%field' => $element['#title'],
        ));
      }
    }
  }
  if (!empty($errors)) {
    $form_state
      ->setError($element, implode(' ', $errors));
  }
}