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

@@ -1,11 +1,34 @@
import MailPoet from 'mailpoet'; import MailPoet from 'mailpoet';
import jQuery from 'jquery'; import jQuery from 'jquery';
import Cookies from 'js-cookie'; import Cookies from 'js-cookie';
import 'parsleyjs'; import Parsley from 'parsleyjs';
const exitIntentEvent = 'mouseleave.mailpoet.form-exit-intent'; const exitIntentEvent = 'mouseleave.mailpoet.form-exit-intent';
jQuery(($) => { jQuery(($) => {
Parsley.addValidator('names', {
requirementType: ['string', 'string'],
validateString: (value, errorBrackets, errorURL) => {
// Name can't contain angle brackets - https://mailpoet.atlassian.net/browse/MAILPOET-3408
const bracketsExpression = /[><]+/gi;
const bracketsRegex = new RegExp(bracketsExpression);
if (value.match(bracketsRegex)) {
return $.Deferred().reject(errorBrackets);
}
// Name can't contain URL - https://mailpoet.atlassian.net/browse/MAILPOET-3786
const urlExpression = /https?:\/\/(www\.)?(.+)\.(.+)/gi;
const urlRegex = new RegExp(urlExpression);
if (value.match(urlRegex)) {
return $.Deferred().reject(errorURL);
}
return true;
},
messages: {
en: 'Please specify a valid name',
},
});
function renderCaptcha(element, iteration) { function renderCaptcha(element, iteration) {
if (!window.recaptcha || !window.grecaptcha.ready) { if (!window.recaptcha || !window.grecaptcha.ready) {
if (iteration < 20) { if (iteration < 20) {

View File

@@ -38,8 +38,13 @@ class BlockRendererHelper {
} }
if (($blockId === 'first_name') || ($blockId === 'last_name')) { if (($blockId === 'first_name') || ($blockId === 'last_name')) {
$rules['pattern'] = "^[^><]*$"; $errorMessages = [
$rules['error-message'] = __('Please specify a valid name', 'mailpoet'); __('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') { if ($blockId === 'segments') {
@@ -85,7 +90,12 @@ class BlockRendererHelper {
if (is_bool($value)) { if (is_bool($value)) {
$value = ($value) ? 'true' : 'false'; $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); return join(' ', $validation);