function ComponentAsFormElementTest::customValidator
Validation callback for a datetime element.
If the date is valid, the date object created from the user input is set in the form for use by the caller. The work of compiling the user input back into a date object is handled by the value callback, so we can use it here. We also have the raw input available for validation testing.
Parameters
array $element: The form element whose value is being validated.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
array $complete_form: The complete form structure.
File
-
core/
tests/ Drupal/ KernelTests/ Components/ ComponentAsFormElementTest.php, line 158
Class
- ComponentAsFormElementTest
- Tests the correct rendering of components in form.
Namespace
Drupal\KernelTests\ComponentsCode
public static function customValidator(&$element, FormStateInterface $form_state, &$complete_form) : void {
$input_exists = FALSE;
$input = NestedArray::getValue($form_state->getValues(), $element['#parents'], $input_exists);
// Example: Only allow 'test_data_valid_value' as valid.
if ($input !== "test_data_valid_value") {
$form_state->setError($element, "Invalid value provided.");
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.