function ValidationTest::testValidateLimitErrors

Tests partial form validation through #limit_validation_errors.

File

core/modules/system/tests/src/Functional/Form/ValidationTest.php, line 91

Class

ValidationTest
Tests form processing and alteration via form validation handlers.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testValidateLimitErrors() {
  $edit = [
    'test' => 'invalid',
    'test_numeric_index[0]' => 'invalid',
    'test_substring[foo]' => 'invalid',
  ];
  $path = 'form-test/limit-validation-errors';
  // Render the form, and verify that the buttons with limited server-side
  // validation have the proper 'formnovalidate' attribute (to prevent
  // client-side validation by the browser).
  $this->drupalGet($path);
  $expected = 'formnovalidate';
  foreach ([
    'partial',
    'partial-numeric-index',
    'substring',
  ] as $type) {
    $element = $this->xpath('//input[@id=:id and @formnovalidate=:expected]', [
      ':id' => 'edit-' . $type,
      ':expected' => $expected,
    ]);
    $this->assertTrue(!empty($element), new FormattableMarkup('The @type button has the proper formnovalidate attribute.', [
      '@type' => $type,
    ]));
  }
  // The button with full server-side validation should not have the
  // 'formnovalidate' attribute.
  $element = $this->xpath('//input[@id=:id and not(@formnovalidate)]', [
    ':id' => 'edit-full',
  ]);
  $this->assertTrue(!empty($element), 'The button with full server-side validation does not have the formnovalidate attribute.');
  // Submit the form by pressing the 'Partial validate' button (uses
  // #limit_validation_errors) and ensure that the title field is not
  // validated, but the #element_validate handler for the 'test' field
  // is triggered.
  $this->drupalPostForm($path, $edit, t('Partial validate'));
  $this->assertNoText(t('@name field is required.', [
    '@name' => 'Title',
  ]));
  $this->assertText('Test element is invalid');
  // Edge case of #limit_validation_errors containing numeric indexes: same
  // thing with the 'Partial validate (numeric index)' button and the
  // 'test_numeric_index' field.
  $this->drupalPostForm($path, $edit, t('Partial validate (numeric index)'));
  $this->assertNoText(t('@name field is required.', [
    '@name' => 'Title',
  ]));
  $this->assertText('Test (numeric index) element is invalid');
  // Ensure something like 'foobar' isn't considered "inside" 'foo'.
  $this->drupalPostForm($path, $edit, t('Partial validate (substring)'));
  $this->assertNoText(t('@name field is required.', [
    '@name' => 'Title',
  ]));
  $this->assertText('Test (substring) foo element is invalid');
  // Ensure not validated values are not available to submit handlers.
  $this->drupalPostForm($path, [
    'title' => '',
    'test' => 'valid',
  ], t('Partial validate'));
  $this->assertText('Only validated values appear in the form values.');
  // Now test full form validation and ensure that the #element_validate
  // handler is still triggered.
  $this->drupalPostForm($path, $edit, t('Full validate'));
  $this->assertText(t('@name field is required.', [
    '@name' => 'Title',
  ]));
  $this->assertText('Test element is invalid');
}

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