function FormElement::validatePattern
Same name in other branches
- 8.9.x core/lib/Drupal/Core/Render/Element/FormElement.php \Drupal\Core\Render\Element\FormElement::validatePattern()
- 10 core/lib/Drupal/Core/Render/Element/FormElement.php \Drupal\Core\Render\Element\FormElement::validatePattern()
- 11.x core/lib/Drupal/Core/Render/Element/FormElement.php \Drupal\Core\Render\Element\FormElement::validatePattern()
#element_validate callback for #pattern form element property.
Parameters
$element: An associative array containing the properties and children of the generic form element.
$form_state: The current state of the form.
array $complete_form: The complete form structure.
File
-
core/
lib/ Drupal/ Core/ Render/ Element/ FormElement.php, line 137
Class
- FormElement
- Provides a base class for form element plugins.
Namespace
Drupal\Core\Render\ElementCode
public static function validatePattern(&$element, FormStateInterface $form_state, &$complete_form) {
if ($element['#value'] !== '') {
// The pattern must match the entire string and should have the same
// behavior as the RegExp object in ECMA 262.
// - Use bracket-style delimiters to avoid introducing a special delimiter
// character like '/' that would have to be escaped.
// - Put in brackets so that the pattern can't interfere with what's
// prepended and appended.
$pattern = '{^(?:' . $element['#pattern'] . ')$}';
if (!preg_match($pattern, $element['#value'])) {
$form_state->setError($element, t('%name field is not in the right format.', [
'%name' => $element['#title'],
]));
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.