diff --git a/core/includes/form.inc b/core/includes/form.inc index efbac99..aaaeb17 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -257,6 +257,12 @@ function template_preprocess_details(&$variables) { $variables['description'] = (!empty($element['#description'])) ? $element['#description'] : ''; $variables['children'] = (isset($element['#children'])) ? $element['#children'] : ''; $variables['value'] = (isset($element['#value'])) ? $element['#value'] : ''; + + // Display any error messages. + $variables['errors'] = NULL; + if (!empty($element['#errors']) && empty($element['#error_no_message'])) { + $variables['errors'] = $element['#errors']; + } } /** diff --git a/core/modules/system/src/Tests/Form/ElementTest.php b/core/modules/system/src/Tests/Form/ElementTest.php index 6555a4f..07cf1e9 100644 --- a/core/modules/system/src/Tests/Form/ElementTest.php +++ b/core/modules/system/src/Tests/Form/ElementTest.php @@ -160,4 +160,13 @@ public function testFormAutocomplete() { $this->assertEqual(count($result), 1, 'Ensure that the user does have access to the autocompletion'); } + /** + * Tests form element error messages. + */ + public function testFormElementErrors() { + $edit = ['error_on_details' => '1']; + $this->drupalPostForm('form-test/group-container', $edit, 'Submit'); + $this->assertText('I am an error on the details element.'); + } + } diff --git a/core/modules/system/templates/details.html.twig b/core/modules/system/templates/details.html.twig index 1c0fd79..cf50eb0 100644 --- a/core/modules/system/templates/details.html.twig +++ b/core/modules/system/templates/details.html.twig @@ -5,6 +5,7 @@ * * Available variables * - attributes: A list of HTML attributes for the details element. + * - errors: (optional) Any errors for this details element, may not be set. * - title: (optional) The title of the element, may not be set. * - description: (optional) The description of the element, may not be set. * - children: (optional) The children of the element, may not be set. @@ -20,6 +21,12 @@ {{ title }} {%- endif -%} + {% if errors %} +
+ {{ errors }} +
+ {% endif %} + {{ description }} {{ children }} {{ value }} diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestGroupContainerForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestGroupContainerForm.php index 26a04c4..5ffd00c 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/FormTestGroupContainerForm.php +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestGroupContainerForm.php @@ -39,12 +39,26 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#type' => 'textfield', '#title' => 'Nest in details element', ); + $form['error_on_details'] = array( + '#type' => 'checkbox', + '#title' => 'Show error on details', + ); + $form['submit'] = array('#type' => 'submit', '#value' => 'Submit'); return $form; } /** * {@inheritdoc} */ + public function validateForm(array &$form, FormStateInterface $form_state) { + if ($form_state->getValue('error_on_details') == True) { + $form_state->setErrorByName('meta', 'I am an error on the details element.'); + } + } + + /** + * {@inheritdoc} + */ public function submitForm(array &$form, FormStateInterface $form_state) { } diff --git a/core/themes/classy/templates/form/details.html.twig b/core/themes/classy/templates/form/details.html.twig index 9b149d7..a4ce6f6 100644 --- a/core/themes/classy/templates/form/details.html.twig +++ b/core/themes/classy/templates/form/details.html.twig @@ -5,6 +5,7 @@ * * Available variables * - attributes: A list of HTML attributes for the details element. + * - errors: (optional) Any errors for this details element, may not be set. * - title: (optional) The title of the element, may not be set. * - description: (optional) The description of the element, may not be set. * - children: (optional) The children of the element, may not be set. @@ -18,6 +19,11 @@ {{ title }} {%- endif -%}
+ {% if errors %} +
+ {{ errors }} +
+ {% endif %} {%- if description -%}
{{ description }}
{%- endif -%}