Skip to content
Snippets Groups Projects
Commit 8f8e6f5b authored by Mikael Meulle's avatar Mikael Meulle Committed by Pierre Dureau
Browse files

Issue #3494336 by just_like_good_vibes, mengi: Validation error with identifier prop type

parent f0735579
No related branches found
No related tags found
1 merge request!301Resolve #3494336 "Validation error with"
Pipeline #371382 canceled
......@@ -26,7 +26,7 @@ class TextfieldWidget extends SourcePluginPropValue implements TrustedCallbackIn
* {@inheritdoc}
*/
public static function trustedCallbacks() {
return ['validatePattern'];
return ['validateUnicodePattern'];
}
/**
......@@ -43,10 +43,11 @@ class TextfieldWidget extends SourcePluginPropValue implements TrustedCallbackIn
* @see https://www.drupal.org/project/drupal/issues/2633550
* @see https://www.drupal.org/project/webform/issues/3002374
*/
public static function validatePattern(array &$element, FormStateInterface $form_state) : void {
public static function validateUnicodePattern(array &$element, FormStateInterface $form_state) : void {
if ($element['#value'] !== '') {
$pattern = $element['#pattern_unicode'] ?? $element['#pattern'];
// JavaScript-escaped Unicode characters to PCRE escape sequence format.
$pcre_pattern = preg_replace('/\\\\u([a-fA-F0-9]{4})/', '\\x{\\1}', $element['#pattern']);
$pcre_pattern = preg_replace('/\\\\u([a-fA-F0-9]{4})/', '\\x{\\1}', $pattern);
$pattern = '{^(?:' . $pcre_pattern . ')$}u';
if (!preg_match($pattern, $element['#value'])) {
if (!empty($element['#pattern_error'])) {
......@@ -71,7 +72,7 @@ class TextfieldWidget extends SourcePluginPropValue implements TrustedCallbackIn
$this->addRequired($form['value']);
$description = [];
if (isset($this->propDefinition["pattern"])) {
$form['value']['#pattern'] = $this->propDefinition["pattern"];
$form['value']['#pattern_unicode'] = $this->propDefinition["pattern"];
$description[] = $this->t("Constraint: @pattern", ["@pattern" => $this->propDefinition["pattern"]]);
}
if (isset($this->propDefinition["maxLength"])) {
......@@ -83,13 +84,14 @@ class TextfieldWidget extends SourcePluginPropValue implements TrustedCallbackIn
$form['value']['#pattern'] = "^.{" . $this->propDefinition["minLength"] . ",}$";
$description[] = $this->t("Min length: @length", ["@length" => $this->propDefinition["minLength"]]);
}
if (isset($form['value']['#pattern']) && !isset($form['value']['#title'])) {
if ((isset($form['value']['#pattern']) || isset($form['value']['#pattern_unicode'])) &&
!isset($form['value']['#title'])) {
$form['value']['#title'] = $this->propDefinition["title"] ?? $this->propId;
}
$form['value']["#description"] = implode("; ", $description);
// @todo change when issue https://www.drupal.org/project/drupal/issues/2633550 is fixed.
if (isset($form['value']['#pattern'])) {
$form['value']['#element_validate'][] = [static::class, 'validatePattern'];
if (isset($form['value']['#pattern_unicode'])) {
$form['value']['#element_validate'][] = [static::class, 'validateUnicodePattern'];
}
return $form;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment