function FormTest::testRequiredTextfieldNoTitle

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::testRequiredTextfieldNoTitle()
  2. 8.9.x core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::testRequiredTextfieldNoTitle()
  3. 11.x core/modules/system/tests/src/Functional/Form/FormTest.php \Drupal\Tests\system\Functional\Form\FormTest::testRequiredTextfieldNoTitle()

Tests validation for required textfield element without title.

Submits a test form containing a textfield form element without title. The form is submitted twice, first without value for the required field and then with value. Each submission is checked for relevant error messages.

See also

\Drupal\form_test\Form\FormTestValidateRequiredNoTitleForm

File

core/modules/system/tests/src/Functional/Form/FormTest.php, line 353

Class

FormTest
Tests various form element validation mechanisms.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testRequiredTextfieldNoTitle() : void {
    // Attempt to submit the form with no required field set.
    $edit = [];
    $this->drupalGet('form-test/validate-required-no-title');
    $this->submitForm($edit, 'Submit');
    $this->assertSession()
        ->pageTextNotContains("The form_test_validate_required_form_no_title form was submitted successfully.");
    // Check the page for the error class on the textfield.
    $this->assertSession()
        ->elementExists('xpath', '//input[contains(@class, "error")]');
    // Check the page for the aria-invalid attribute on the textfield.
    $this->assertSession()
        ->elementExists('xpath', '//input[contains(@aria-invalid, "true")]');
    // Submit again with required fields set and verify that there are no
    // error messages.
    $edit = [
        'textfield' => $this->randomString(),
    ];
    $this->submitForm($edit, 'Submit');
    // Verify that no error input form element class is present.
    $this->assertSession()
        ->elementNotExists('xpath', '//input[contains(@class, "error")]');
    $this->assertSession()
        ->pageTextContains("The form_test_validate_required_form_no_title form was submitted successfully.");
}

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