diff --git a/core/includes/common.inc b/core/includes/common.inc index 08ce2ab..55661d9 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -7031,6 +7031,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 b5a13c5..8f52c6d 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -858,7 +858,18 @@ 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']) { + $errors = form_get_errors(); + if ($errors) { + // $form_state['errors'] = $errors; + $error_links = array(); + foreach ($errors as $key => $error) { + $element = form_get_element($key, $form); + $title = isset($element['#title']) ? $element['#title'] : $key; + $error_links[] = l($title, '', array('fragment' => 'edit-' . str_replace('_', '-', $key), 'external' => TRUE)); + } + drupal_set_message(format_plural(count($error_links), '1 error has been found', '@count errors have been found') . ': ' . implode(', ', $error_links), 'error'); + } + elseif ($form_state['submitted'] && !$form_state['rebuild']) { // Execute form submit handlers. form_execute_handlers('submit', $form, $form_state); @@ -926,7 +937,7 @@ function drupal_process_form($form_id, &$form, &$form_state) { // along with element-level #submit properties, it makes no sense to have // divergent form execution based on whether the triggering element has // #executes_submit_callback set to TRUE. - if (($form_state['rebuild'] || !$form_state['executed']) && !form_get_errors()) { + if (($form_state['rebuild'] || !$form_state['executed']) && !$errors) { // Form building functions (e.g., _form_builder_handle_input_element()) // may use $form_state['rebuild'] to determine if they are running in the // context of a rebuild, so ensure it is set. @@ -1587,9 +1598,6 @@ function form_set_error($name = NULL, $message = '', $limit_validation_errors = } if ($record) { $form[$name] = $message; - if ($message) { - drupal_set_message($message, 'error'); - } } } @@ -1921,6 +1929,37 @@ 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) { @@ -4266,6 +4305,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']; @@ -4282,8 +4323,19 @@ function theme_form_element($variables) { if (!empty($element['#attributes']['disabled'])) { $attributes['class'][] = 'form-disabled'; } + // Add a class if an error exists. + if ($variables['errors']) { + $attributes['class'][] = 'form-error'; + $attributes['class'][] = 'messages'; + $attributes['class'][] = 'error'; + } $output = '' . "\n"; + // Display any error messages. + if ($variables['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'; @@ -4399,6 +4451,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.