diff --git a/core/includes/form.inc b/core/includes/form.inc index 513615c..df13fba 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -964,6 +964,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 array $form + * An associative array containing the structure of the form. + */ +function form_display_errors(array $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(array $element) { + if (isset($element['#title'])) { + return $element['#title']; + } + else { + foreach (element_children($element) as $key) { + $title = form_element_get_title($element[$key]); + if (isset($title)) { + return $title; + } + } + } +} + +/** * Prepares a structured form array. * * Adds required elements, executes any hook_form_alter functions, and @@ -1193,6 +1239,10 @@ function drupal_validate_form($form_id, &$form, &$form_state) { } $form_state['values'] = $values; } + + if (!$form_state['programmed']) { + form_display_errors($form); + } } /** @@ -1634,9 +1684,6 @@ function form_set_error($name = NULL, $message = '', $limit_validation_errors = } if ($record) { $form[$name] = $message; - if ($message) { - drupal_set_message($message, 'error'); - } } } @@ -1792,6 +1839,7 @@ function form_builder($form_id, &$element, &$form_state) { '#required' => FALSE, '#attributes' => array(), '#title_display' => 'before', + '#error_use_parent' => FALSE, ); // Special handling if we're on the top level form element. @@ -1965,6 +2013,38 @@ 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) { @@ -2962,6 +3042,7 @@ function form_process_password_confirm($element) { '#value' => empty($element['#value']) ? NULL : $element['#value']['pass1'], '#required' => $element['#required'], '#attributes' => array('class' => array('password-field')), + '#error_use_parent' => TRUE, ); $element['pass2'] = array( '#type' => 'password', @@ -2969,6 +3050,7 @@ function form_process_password_confirm($element) { '#value' => empty($element['#value']) ? NULL : $element['#value']['pass2'], '#required' => $element['#required'], '#attributes' => array('class' => array('password-confirm')), + '#error_use_parent' => TRUE, ); $element['#element_validate'] = array('password_confirm_validate'); $element['#tree'] = TRUE; @@ -3161,6 +3243,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. + '#error_use_parent' => TRUE, '#weight' => $weight, ); } @@ -3312,6 +3396,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. + '#error_use_parent' => TRUE, '#weight' => $weight, ); } @@ -4675,6 +4761,8 @@ function theme_form_element($variables) { '#title_display' => 'before', ); + $variables['errors'] = form_get_error($element); + // Take over any #wrapper_attributes defined by the element. // @todo Temporary hack for #type 'item'. // @see http://drupal.org/node/1829202 @@ -4697,8 +4785,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 = '