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:
@@ -1,11 +1,34 @@
|
||||
import MailPoet from 'mailpoet';
|
||||
import jQuery from 'jquery';
|
||||
import Cookies from 'js-cookie';
|
||||
import 'parsleyjs';
|
||||
import Parsley from 'parsleyjs';
|
||||
|
||||
const exitIntentEvent = 'mouseleave.mailpoet.form-exit-intent';
|
||||
|
||||
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) {
|
||||
if (!window.recaptcha || !window.grecaptcha.ready) {
|
||||
if (iteration < 20) {
|
||||
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user