function FormErrorHandlerTest::testDisplayErrorMessages

Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Form/FormErrorHandlerTest.php \Drupal\Tests\Core\Form\FormErrorHandlerTest::testDisplayErrorMessages()
  2. 10 core/tests/Drupal/Tests/Core/Form/FormErrorHandlerTest.php \Drupal\Tests\Core\Form\FormErrorHandlerTest::testDisplayErrorMessages()
  3. 11.x core/tests/Drupal/Tests/Core/Form/FormErrorHandlerTest.php \Drupal\Tests\Core\Form\FormErrorHandlerTest::testDisplayErrorMessages()

@covers ::handleFormErrors @covers ::displayErrorMessages

File

core/tests/Drupal/Tests/Core/Form/FormErrorHandlerTest.php, line 50

Class

FormErrorHandlerTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Form%21FormErrorHandler.php/class/FormErrorHandler/9" title="Handles form errors." class="local">\Drupal\Core\Form\FormErrorHandler</a> @group Form

Namespace

Drupal\Tests\Core\Form

Code

public function testDisplayErrorMessages() {
    $this->messenger
        ->expects($this->exactly(6))
        ->method('addMessage')
        ->withConsecutive([
        'invalid',
        'error',
    ], [
        'invalid',
        'error',
    ], [
        'invalid',
        'error',
    ], [
        'no title given',
        'error',
    ], [
        'element is invisible',
        'error',
    ], [
        'this missing element is invalid',
        'error',
    ]);
    $form = [
        '#parents' => [],
        '#array_parents' => [],
    ];
    $form['test1'] = [
        '#type' => 'textfield',
        '#title' => 'Test 1',
        '#parents' => [
            'test1',
        ],
        '#array_parents' => [
            'test1',
        ],
        '#id' => 'edit-test1',
    ];
    $form['test2'] = [
        '#type' => 'textfield',
        '#title' => 'Test 2 & a half',
        '#parents' => [
            'test2',
        ],
        '#array_parents' => [
            'test2',
        ],
        '#id' => 'edit-test2',
    ];
    $form['fieldset'] = [
        '#parents' => [
            'fieldset',
        ],
        '#array_parents' => [
            'fieldset',
        ],
        'test3' => [
            '#type' => 'textfield',
            '#title' => 'Test 3',
            '#parents' => [
                'fieldset',
                'test3',
            ],
            '#array_parents' => [
                'fieldset',
                'test3',
            ],
            '#id' => 'edit-test3',
        ],
    ];
    $form['test5'] = [
        '#type' => 'textfield',
        '#parents' => [
            'test5',
        ],
        '#array_parents' => [
            'test5',
        ],
        '#id' => 'edit-test5',
    ];
    $form['test6'] = [
        '#type' => 'value',
        '#title' => 'Test 6',
        '#parents' => [
            'test6',
        ],
        '#array_parents' => [
            'test6',
        ],
        '#id' => 'edit-test6',
    ];
    $form_state = new FormState();
    $form_state->setErrorByName('test1', 'invalid');
    $form_state->setErrorByName('test2', 'invalid');
    $form_state->setErrorByName('fieldset][test3', 'invalid');
    $form_state->setErrorByName('test5', 'no title given');
    $form_state->setErrorByName('test6', 'element is invisible');
    $form_state->setErrorByName('missing_element', 'this missing element is invalid');
    $this->formErrorHandler
        ->handleFormErrors($form, $form_state);
    $this->assertSame('invalid', $form['test1']['#errors']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.