function DatetimeElementFormTest::testDatetimeElementRequiredError

Tests the custom required error message for datetime elements.

File

core/tests/Drupal/KernelTests/Core/Datetime/DatetimeElementFormTest.php, line 239

Class

DatetimeElementFormTest
Tests DatetimeElement functionality.

Namespace

Drupal\KernelTests\Core\Datetime

Code

public function testDatetimeElementRequiredError() : void {
    $form_builder = $this->container
        ->get('form_builder');
    // Test datetime element with #required_error.
    $form_state = (new FormState())->setValues([
        'datetime_required_error' => '',
    ]);
    $form_builder->submitForm($this, $form_state);
    // Check that the custom required error message is set correctly.
    $this->assertEquals('Custom required error message.', $form_state->getErrors()['datetime_required_error']);
    // Test datetime element without #required_error.
    $form_state = (new FormState())->setValues([
        'datetime_no_required_error' => '',
    ]);
    $form_builder->submitForm($this, $form_state);
    // Check that the default required error message is set correctly.
    $this->assertEquals('The Datetime without required error date is required.', $form_state->getErrors()['datetime_no_required_error']);
}

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