function FormTest::testRequiredCheckboxesRadio
Same name in other branches
- 9 core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::testRequiredCheckboxesRadio()
- 8.9.x core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::testRequiredCheckboxesRadio()
- 11.x core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::testRequiredCheckboxesRadio()
Tests validation for required checkbox, select, and radio elements.
Submits a test form containing several types of form elements. The form is submitted twice, first without values for required fields and then with values. Each submission is checked for relevant error messages.
See also
\Drupal\form_test\Form\FormTestValidateRequiredForm
File
-
core/
modules/ system/ tests/ src/ Functional/ Form/ FormTest.php, line 173
Class
- FormTest
- Tests various form element validation mechanisms.
Namespace
Drupal\Tests\system\Functional\FormCode
public function testRequiredCheckboxesRadio() : void {
$form = \Drupal::formBuilder()->getForm('\\Drupal\\form_test\\Form\\FormTestValidateRequiredForm');
// Attempt to submit the form with no required fields set.
$edit = [];
$this->drupalGet('form-test/validate-required');
$this->submitForm($edit, 'Submit');
// The only error messages that should appear are the relevant 'required'
// messages for each field.
$expected = [];
foreach ([
'textfield',
'checkboxes',
'select',
'radios',
] as $key) {
if (isset($form[$key]['#required_error'])) {
$expected[] = $form[$key]['#required_error'];
}
elseif (isset($form[$key]['#form_test_required_error'])) {
$expected[] = $form[$key]['#form_test_required_error'];
}
else {
$expected[] = $form[$key]['#title'] . ' field is required.';
}
}
// Check the page for error messages.
$errors = $this->xpath('//div[contains(@class, "error")]//li');
foreach ($errors as $error) {
$expected_key = array_search($error->getText(), $expected);
// If the error message is not one of the expected messages, fail.
if ($expected_key === FALSE) {
$this->fail(new FormattableMarkup("Unexpected error message: @error", [
'@error' => $error[0],
]));
}
else {
unset($expected[$expected_key]);
}
}
// Fail if any expected messages were not found.
foreach ($expected as $not_found) {
$this->fail(new FormattableMarkup("Found error message: @error", [
'@error' => $not_found,
]));
}
// Verify that input elements are still empty.
$this->assertSession()
->fieldValueEquals('textfield', '');
$this->assertSession()
->checkboxNotChecked('edit-checkboxes-foo');
$this->assertSession()
->checkboxNotChecked('edit-checkboxes-bar');
$this->assertTrue($this->assertSession()
->optionExists('edit-select', '')
->isSelected());
$this->assertSession()
->checkboxNotChecked('edit-radios-foo');
$this->assertSession()
->checkboxNotChecked('edit-radios-bar');
$this->assertSession()
->checkboxNotChecked('edit-radios-optional-foo');
$this->assertSession()
->checkboxNotChecked('edit-radios-optional-bar');
$this->assertSession()
->checkboxNotChecked('edit-radios-optional-default-value-false-foo');
$this->assertSession()
->checkboxNotChecked('edit-radios-optional-default-value-false-bar');
// Submit again with required fields set and verify that there are no
// error messages.
$edit = [
'textfield' => $this->randomString(),
'checkboxes[foo]' => TRUE,
'select' => 'foo',
'radios' => 'bar',
];
$this->submitForm($edit, 'Submit');
// Verify that no error message is displayed when all required fields are
// filled.
$this->assertSession()
->elementNotExists('xpath', '//div[contains(@class, "error")]');
$this->assertSession()
->pageTextContains("The form_test_validate_required_form form was submitted successfully.");
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.