Create custom validation for names

I had to use a custom validation because we want to different
error message for each part of validation.
I didn't find a better solution for Parsley library.

[MAILPOET-3786]
This commit is contained in:
Jan Lysý
2021-09-14 09:37:12 +02:00
committed by Veljko V
parent ec86e742c2
commit f9ba4623c6
2 changed files with 37 additions and 4 deletions

View File

@@ -38,8 +38,13 @@ class BlockRendererHelper {
}
if (($blockId === 'first_name') || ($blockId === 'last_name')) {
$rules['pattern'] = "^[^><]*$";
$rules['error-message'] = __('Please specify a valid name', 'mailpoet');
$errorMessages = [
__('Please specify a valid name', 'mailpoet'),
__('Addresses in names are not permitted, please add your name instead.', 'mailpoet'),
];
$rules['names'] = '[' . implode(',', array_map(function (string $errorMessage): string {
return htmlspecialchars((string)json_encode($errorMessage), ENT_QUOTES);
}, $errorMessages)) . ']';
}
if ($blockId === 'segments') {
@@ -85,7 +90,12 @@ class BlockRendererHelper {
if (is_bool($value)) {
$value = ($value) ? 'true' : 'false';
}
$validation[] = 'data-parsley-' . $rule . '="' . $value . '"';
// We need to use single quotes because we need to pass array of strings as a parameter for custom validation
if ($rule === 'names') {
$validation[] = 'data-parsley-' . $rule . '=\'' . $value . '\'';
} else {
$validation[] = 'data-parsley-' . $rule . '="' . $value . '"';
}
}
}
return join(' ', $validation);