#pattern fails for Unicode points

Created on 11 December 2015, about 9 years ago
Updated 17 December 2024, 7 days ago

Let's say you want to include some Unicode character points in your pattern.

$form['my_element'] = array(
  '#title' => t('My cool field'),
  '#type' => 'text',
  '#pattern' => '[\u00C0-\u00D6]',
);

If you go use the form and type in "ÇÁ" and submit the form. The JS validation passes so the submission goes through but then on the server side, a PHP warning gets raised from FormElement::validatePattern() and the validation fails there.

preg_match(): Compilation failed: PCRE does not support \L, \l, \N{name}, \U, or \u at offset NN in Drupal\Core\Render\Element\FormElement::validatePattern() (line 143 of core/lib/Drupal/Core/Render/Element/FormElement.php).

See #4:
\Drupal\Core\Render\Element\FormElement::validatePattern() seems to indicate that it should be okay:

// The pattern must match the entire string and should have the same
// behavior as the RegExp object in ECMA 262.
Both ECMA 262 and PCRE allow you craft a regex to target specific Unicode items, but they differ (\uhhhh and \x{hhhh} respectively), so if you use a #pattern that passes for one of them, it will fail for the other.

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component

forms system

Created by

πŸ‡ΊπŸ‡ΈUnited States kevin.dutra

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024