diff --git a/core/includes/common.inc b/core/includes/common.inc index 15c54d3..292abb8 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -6914,6 +6914,9 @@ function drupal_common_theme() { 'form' => array( 'render element' => 'element', ), + 'form_error_message' => array( + 'render element' => 'element', + ), 'textarea' => array( 'render element' => 'element', ), diff --git a/core/includes/form.inc b/core/includes/form.inc index c94bc62..5965f3c 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -858,7 +858,7 @@ function drupal_process_form($form_id, &$form, &$form_state) { // browser don't increment all the element IDs needlessly. drupal_static_reset('drupal_html_id'); - if ($form_state['submitted'] && !form_get_errors() && !$form_state['rebuild']) { + if ($form_state['submitted'] && !$form_state['rebuild'] && !form_get_errors()) { // Execute form submit handlers. form_execute_handlers('submit', $form, $form_state); @@ -902,9 +902,16 @@ function drupal_process_form($form_id, &$form, &$form_state) { // Set a flag to indicate the the form has been processed and executed. $form_state['executed'] = TRUE; + // Occassionally a form will set an error in its submit handler so we make + // sure to display those errors here before the form is redirected. + form_display_errors($form); + // Redirect the form based on values in $form_state. drupal_redirect_form($form_state); } + else { + form_display_errors($form); + } // Don't rebuild or cache form submissions invoked via drupal_form_submit(). if (!empty($form_state['programmed'])) { @@ -948,6 +955,52 @@ function drupal_process_form($form_id, &$form, &$form_state) { } /** + * Displays the given form's errors and links each error to the form element + * in question. + * + * @param $form + * An associative array containing the structure of the form. + */ +function form_display_errors($form) { + if ($errors = form_get_errors()) { + $error_links = array(); + foreach ($errors as $key => $error) { + $element = form_get_element($key, $form); + if ($element) { + $title = form_element_get_title($element); + $error_links[] = l($title, '', array('fragment' => 'edit-' . str_replace('_', '-', $key), 'external' => TRUE)); + } + else { + drupal_set_message($error, 'error'); + unset($errors[$key]); + } + } + + if (!empty($error_links)) { + drupal_set_message(format_plural(count($error_links), '1 error has been found', '@count errors have been found') . ': ' . implode(', ', $error_links), 'error'); + } + } +} + +/** + * Returns the title the highest element in the hierarchy that has a title. If + * no title is found, then NULL is returned. + */ +function form_element_get_title($element) { + if (isset($element['#title'])) { + return $element['#title']; + } + else { + foreach (element_children($element) as $key) { + $title = form_element_get_title($element[$key]); + if (!is_null($title)) { + return $title; + } + } + } +} + +/** * Prepares a structured form array. * * Adds required elements, executes any hook_form_alter functions, and @@ -1175,6 +1228,7 @@ function drupal_validate_form($form_id, &$form, &$form_state) { $values[$name] = $button_value; } } + $form_state['values'] = $values; } } @@ -1599,9 +1653,6 @@ function form_set_error($name = NULL, $message = '', $limit_validation_errors = } if ($record) { $form[$name] = $message; - if ($message) { - drupal_set_message($message, 'error'); - } } } @@ -1634,11 +1685,13 @@ function form_get_errors() { function form_get_error($element) { $form = form_set_error(); $parents = array(); - foreach ($element['#parents'] as $parent) { - $parents[] = $parent; - $key = implode('][', $parents); - if (isset($form[$key])) { - return $form[$key]; + if (isset($element['#parents'])) { + foreach ($element['#parents'] as $parent) { + $parents[] = $parent; + $key = implode('][', $parents); + if (isset($form[$key])) { + return $form[$key]; + } } } } @@ -1757,6 +1810,7 @@ function form_builder($form_id, &$element, &$form_state) { '#required' => FALSE, '#attributes' => array(), '#title_display' => 'before', + '#hide_errors' => FALSE, ); // Special handling if we're on the top level form element. @@ -1933,6 +1987,39 @@ function form_builder($form_id, &$element, &$form_state) { } /** + * Given a form and an element key, this function returns the element no matter + * how deep within the form array the key exists. If the key is not found an + * empty array is returned. + * + * @param string $element_key + * The key to search for. + * + * @param array $form + * A structured form array to search. + * + * @return Array + */ +function form_get_element($element_key, $form) { + $element = array(); + foreach (element_children($form) as $key) { + if ($key === $element_key) { + $element = $form[$key]; + break; + } + else { + if (is_array($form[$key])) { + $element = form_get_element($element_key, $form[$key]); + if (!empty($element)) { + break; + } + } + } + } + + return $element; +} + +/** * Adds the #name and #value properties of an input element before rendering. */ function _form_builder_handle_input_element($form_id, &$element, &$form_state) { @@ -2850,6 +2937,7 @@ function form_process_password_confirm($element) { '#value' => empty($element['#value']) ? NULL : $element['#value']['pass1'], '#required' => $element['#required'], '#attributes' => array('class' => array('password-field')), + '#hide_errors' => TRUE, ); $element['pass2'] = array( '#type' => 'password', @@ -2857,6 +2945,7 @@ function form_process_password_confirm($element) { '#value' => empty($element['#value']) ? NULL : $element['#value']['pass2'], '#required' => $element['#required'], '#attributes' => array('class' => array('password-confirm')), + '#hide_errors' => TRUE, ); $element['#element_validate'] = array('password_confirm_validate'); $element['#tree'] = TRUE; @@ -3048,6 +3137,8 @@ function form_process_radios($element) { '#parents' => $element['#parents'], '#id' => drupal_html_id('edit-' . implode('-', $parents_for_id)), '#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL, + // Errors should only be shown on the parent radios element. + '#hide_errors' => TRUE, '#weight' => $weight, ); } @@ -3201,6 +3292,8 @@ function form_process_checkboxes($element) { '#default_value' => isset($value[$key]) ? $key : NULL, '#attributes' => $element['#attributes'], '#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL, + // Errors should only be shown on the parent checkboxes element. + '#hide_errors' => TRUE, '#weight' => $weight, ); } @@ -4358,6 +4451,8 @@ function theme_form_element($variables) { '#title_display' => 'before', ); + $variables['errors'] = form_get_error($element); + // Add element #id for #type 'item'. if (isset($element['#markup']) && !empty($element['#id'])) { $attributes['id'] = $element['#id']; @@ -4374,8 +4469,17 @@ function theme_form_element($variables) { if (!empty($element['#attributes']['disabled'])) { $attributes['class'][] = 'form-disabled'; } + // Add a class if an error exists. + if (!empty($variables['errors'])) { + $attributes['class'][] = 'form-error'; + } $output = '' . "\n"; + // Display any error messages. + if ($variables['errors'] && !$element['#hide_errors']) { + $output .= ' ' . theme('form_error_message', $variables); + } + // If #title is not set, we don't display any label or required marker. if (!isset($element['#title'])) { $element['#title_display'] = 'none'; @@ -4491,6 +4595,26 @@ function theme_form_element_label($variables) { } /** + * Returns HTML for an inline error associated with a specific form element. + * + * @param $variables + * An associative array containing: + * - element: An associative array containing the properties of the element. + * Properties used: error. + * - errors: The errors associated with the current element as returned by + * form_get_error($element). + * + * @ingroup themeable + */ +function theme_form_error_message($variables) { + $output = '
'; + $output .= '' . t('Error') . ': ' . $variables['errors'] . ''; + $output .= '
'; + + return $output; +} + +/** * Sets a form element's class attribute. * * Adds 'required' and 'error' classes as needed. diff --git a/core/modules/system/system.theme.css b/core/modules/system/system.theme.css index d5f41e1..151b371 100644 --- a/core/modules/system/system.theme.css +++ b/core/modules/system/system.theme.css @@ -75,6 +75,16 @@ td.active { /** * Markup generated by Form API. */ +div.form-error { + background-color: #fef5f1; + border: 1px solid #ed541d; + color: #8c2e0b; + padding: 10px; +} +div.form-error-message { + margin-bottom: 10px; + min-height: 25px; +} .form-item, .form-actions { margin-top: 1em;