public function FormErrorHandlerTest::testSetElementErrorsFromFormState in Drupal 8
@covers ::handleFormErrors @covers ::setElementErrorsFromFormState
File
- core/
tests/ Drupal/ Tests/ Core/ Form/ FormErrorHandlerTest.php, line 127
Class
- FormErrorHandlerTest
- @coversDefaultClass \Drupal\Core\Form\FormErrorHandler @group Form
Namespace
Drupal\Tests\Core\FormCode
public function testSetElementErrorsFromFormState() {
$form = [
'#parents' => [],
'#array_parents' => [],
];
$form['test'] = [
'#type' => 'textfield',
'#title' => 'Test',
'#parents' => [
'test',
],
'#array_parents' => [
'test',
],
'#id' => 'edit-test',
];
$form['details'] = [
'#type' => 'details',
'#title' => 'Details grouping test',
'#parents' => [
'details',
],
'#array_parents' => [
'details',
],
'#id' => 'edit-details',
];
$form['grouping_test'] = [
'#type' => 'textfield',
'#title' => 'Grouping test',
'#parents' => [
'grouping_test',
],
'#array_parents' => [
'grouping_test',
],
'#id' => 'edit-grouping-test',
'#group' => 'details',
];
$form['grouping_test2'] = [
'#type' => 'textfield',
'#title' => 'Grouping test 2',
'#parents' => [
'grouping_test2',
],
'#array_parents' => [
'grouping_test2',
],
'#id' => 'edit-grouping-test2',
'#group' => 'details',
];
$form['details2'] = [
'#type' => 'details',
'#title' => 'Details grouping test 2',
'#parents' => [
'details2',
],
'#array_parents' => [
'details2',
],
'#id' => 'edit-details2',
];
$form['grouping_test3'] = [
'#type' => 'textfield',
'#title' => 'Grouping test 3',
'#parents' => [
'grouping_test3',
],
'#array_parents' => [
'grouping_test3',
],
'#id' => 'edit-grouping-test3',
'#group' => 'details2',
];
$form['fieldset'] = [
'#type' => 'fieldset',
'#parents' => [
'fieldset',
],
'#array_parents' => [
'fieldset',
],
'#id' => 'edit-fieldset',
'nested_test' => [
'#type' => 'textfield',
'#title' => 'Nested test',
'#parents' => [
'fieldset',
'nested_test',
],
'#array_parents' => [
'fieldset',
'nested_test',
],
'#id' => 'edit-nested_test',
],
'nested_test2' => [
'#type' => 'textfield',
'#title' => 'Nested test2',
'#parents' => [
'fieldset',
'nested_test2',
],
'#array_parents' => [
'fieldset',
'nested_test2',
],
'#id' => 'edit-nested_test2',
],
];
$form_state = new FormState();
$form_state
->setErrorByName('test', 'invalid');
$form_state
->setErrorByName('grouping_test', 'invalid');
$form_state
->setErrorByName('grouping_test2', 'invalid');
$form_state
->setErrorByName('fieldset][nested_test', 'invalid');
$form_state
->setErrorByName('fieldset][nested_test2', 'invalid2');
$this->formErrorHandler
->handleFormErrors($form, $form_state);
$this
->assertSame('invalid', $form['test']['#errors']);
$this
->assertSame([
'grouping_test' => 'invalid',
'grouping_test2' => 'invalid',
], $form['details']['#children_errors']);
$this
->assertSame([
'fieldset][nested_test' => 'invalid',
'fieldset][nested_test2' => 'invalid2',
], $form['fieldset']['#children_errors']);
$this
->assertEmpty($form['details2']['#children_errors'], 'Children errors are empty for grouping element.');
$this
->assertCount(5, $form['#children_errors']);
}