You are here

function ife_element_errors_set in Inline Form Errors 7.2

General function to check if a form element has an error. This function will then set the error on the element to be later used by our theming function theme_ife_form_element().

1 call to ife_element_errors_set()
ife_form_validator in ./ife.module
Custom form validation handler.

File

./ife.module, line 259
Drupal hooks.

Code

function ife_element_errors_set(&$element, $display, $carry_down = FALSE) {
  if (!isset($_SESSION['messages']['error'])) {
    return;
  }

  // Recurse through all children.
  foreach (element_children($element) as $key) {
    if (isset($element[$key]) && $element[$key]) {
      ife_element_errors_set($element[$key], $display, $carry_down);
    }
  }

  // Check for errors and settings.
  $errors = form_get_errors();
  if (!isset($element['#parents'])) {
    return;
  }
  $element_id = implode('][', $element['#parents']);
  if (!empty($errors[$element_id])) {
    $error_message = $errors[$element_id];

    // Get error id.
    $error_id = array_search($error_message, $_SESSION['messages']['error']);
    if ($error_id !== FALSE) {
      if (isset($display) && $display != IFE_MESSAGE_LEAVE) {
        unset($_SESSION['messages']['error'][$error_id]);
        $_SESSION['messages']['error'] = array_values($_SESSION['messages']['error']);
      }
      if (count($_SESSION['messages']['error']) <= 0) {
        unset($_SESSION['messages']['error']);
      }

      // Add error message to the element.
      $element['#ife_error'] = $error_message;

      // Add the position for the inline error message.
      $element['#ife_error_position'] = variable_get('ife_position_inline_message', IFE_POSITION_INLINE_MESSAGE_AFTER);

      // Add in our theme wrapper on the fly if it's not custom placement.
      if ($element['#ife_error_position'] != IFE_POSITION_INLINE_MESSAGE_CUSTOM) {
        if (!isset($element['#theme_wrappers']) || !in_array('ife_form_element', $element['#theme_wrappers'])) {
          $element['#theme_wrappers'][] = 'ife_form_element';
        }
      }

      // Found a matching error, no need to continue.
      return;
    }
  }
}