diff --git a/ife.module b/ife.module old mode 100644 new mode 100755 index 5e738b6..44a4446 --- a/ife.module +++ b/ife.module @@ -258,39 +258,50 @@ function ife_errors($op = 'get', $id = NULL, $message = NULL) { } } -function ife_element_errors_set($element, $display) { - if (!isset($_SESSION['messages'])) { - return; - } - - // Recurse through all children. - foreach (element_children($element) as $key) { - if (isset($element[$key]) && $element[$key]) { - ife_element_errors_set($element[$key], $display); - } - } - - // Check for errors and settings - $errors = form_get_errors(); - $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']); +/** + * Associate error messages with the fields they've been raised against. + * + * @param array $elements + * The elements to check for error messages. + * @param int $display + * What to do with the error messages raised by Drupal. + * - 0: Leave the messages in place. + * - Anything else: Remove all messages. + * @param bool $top = TRUE + * This parameter is used internally to detect when we've finished scanning + * the form so that we can clean up the session variables. + */ +function ife_element_errors_set($elements, $display, $top = TRUE) { + // Loop through each of the child elements of the form. + foreach (element_children($elements) as $key) { + // If the element has an error associated with it, we added it and stop + // descending down this branch of the tree. + if ($error_message = form_get_error($elements[$key])) { + // Set error message in session, so it can be used in our theming. + ife_errors('set', $elements[$key]['#id'], $error_message); - if ($error_id !== FALSE) { - if (isset($display) && $display != 0) { + // Check to see if the error has been set in the session also and remove + // it if necessary. + $error_id = array_search($error_message, $_SESSION['messages']['error']); + if ($error_id !== FALSE && isset($display) && $display != 0) { unset($_SESSION['messages']['error'][$error_id]); - $_SESSION['messages']['error'] = array_values($_SESSION['messages']['error']); - } - - if (count($_SESSION['messages']['error']) <= 0) { - unset($_SESSION['messages']['error']); } + } + // If there isn't an error then we repeat the process with the children of + // this element. + else { + ife_element_errors_set($elements[$key], $elements, FALSE); + } + } - // Set error message in session, so it can be used in our theming. - ife_errors('set', $element['#id'], $error_message); + // Finally clean up the session variable. Remove the error array if it's + // empty, otherwise renumber the keys. + if ($top && isset($_SESSION['messages']['error'])) { + if (count($_SESSION['messages']['error']) == 0) { + unset($_SESSION['messages']['error']); + } + else { + $_SESSION['messages']['error'] = array_values($_SESSION['messages']['error']); } } }