Skip to content

Commit c21229c

Browse files
author
Carlos Lizaga
committed
New validation: validate-no-utf8mb4-characters.
1 parent b64e470 commit c21229c

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

app/code/Magento/Catalog/view/frontend/templates/product/view/options/type/text.phtml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ $class = ($_option->getIsRequire()) ? ' required' : '';
2929
if ($_option->getMaxCharacters()) {
3030
$_textValidate['maxlength'] = $_option->getMaxCharacters();
3131
}
32+
$_textValidate['validate-no-utf8mb4-characters'] = true;
3233
?>
3334
<input type="text"
3435
id="options_<?= /* @escapeNotVerified */ $_option->getId() ?>_text"
@@ -47,6 +48,7 @@ $class = ($_option->getIsRequire()) ? ' required' : '';
4748
if ($_option->getMaxCharacters()) {
4849
$_textAreaValidate['maxlength'] = $_option->getMaxCharacters();
4950
}
51+
$_textAreaValidate['validate-no-utf8mb4-characters'] = true;
5052
?>
5153
<textarea id="options_<?= /* @escapeNotVerified */ $_option->getId() ?>_text"
5254
class="product-custom-option"

dev/tests/js/jasmine/tests/lib/mage/validation.test.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,76 @@ define([
183183
)).toEqual(true);
184184
});
185185
});
186+
187+
describe('Testing 3 bytes characters only policy (UTF-8)', function () {
188+
it('rejects data, if any of the characters cannot be stored using UTF-8 collation', function () {
189+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
190+
$.validator.prototype, '😅😂', null
191+
)).toEqual(false);
192+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
193+
$.validator.prototype, '😅 test 😂', null
194+
)).toEqual(false);
195+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
196+
$.validator.prototype, '💩 👻 💀', null
197+
)).toEqual(false);
198+
});
199+
200+
it('approves data, if all the characters can be stored using UTF-8 collation', function () {
201+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
202+
$.validator.prototype, '', null
203+
)).toEqual(true);
204+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
205+
$.validator.prototype, '!$-_%ç&#?!', null
206+
)).toEqual(true);
207+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
208+
$.validator.prototype, '1234567890', null
209+
)).toEqual(true);
210+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
211+
$.validator.prototype, ' ', null
212+
)).toEqual(true);
213+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
214+
$.validator.prototype, 'test', null
215+
)).toEqual(true);
216+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
217+
$.validator.prototype, 'испытание', null
218+
)).toEqual(true);
219+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
220+
$.validator.prototype, 'тест', null
221+
)).toEqual(true);
222+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
223+
$.validator.prototype, 'փորձարկում', null
224+
)).toEqual(true);
225+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
226+
$.validator.prototype, 'परीक्षण', null
227+
)).toEqual(true);
228+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
229+
$.validator.prototype, 'テスト', null
230+
)).toEqual(true);
231+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
232+
$.validator.prototype, '테스트', null
233+
)).toEqual(true);
234+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
235+
$.validator.prototype, '测试', null
236+
)).toEqual(true);
237+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
238+
$.validator.prototype, '測試', null
239+
)).toEqual(true);
240+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
241+
$.validator.prototype, 'ทดสอบ', null
242+
)).toEqual(true);
243+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
244+
$.validator.prototype, 'δοκιμή', null
245+
)).toEqual(true);
246+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
247+
$.validator.prototype, 'اختبار', null
248+
)).toEqual(true);
249+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
250+
$.validator.prototype, 'تست', null
251+
)).toEqual(true);
252+
expect($.validator.methods['validate-no-utf8mb4-characters'].call(
253+
$.validator.prototype, 'מִבְחָן', null
254+
)).toEqual(true);
255+
});
256+
});
257+
186258
});

lib/web/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ Submit,Submit
9999
"Password cannot be the same as email address.","Password cannot be the same as email address."
100100
"Please fix this field.","Please fix this field."
101101
"Please enter a valid email address.","Please enter a valid email address."
102+
"Please remove invalid characters: {0}.", "Please remove invalid characters: {0}."
102103
"Please enter a valid URL.","Please enter a valid URL."
103104
"Please enter a valid date (ISO).","Please enter a valid date (ISO)."
104105
"Please enter only digits.","Please enter only digits."

lib/web/mage/validation.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,24 @@
396396
$.mage.__('Please enter at least {0} characters')
397397
],
398398

399+
/* detect chars that would require more than 3 bytes */
400+
'validate-no-utf8mb4-characters': [
401+
function (value) {
402+
var validator = this,
403+
message = $.mage.__('Please remove invalid characters: {0}.'),
404+
matches = value.match(/(?:[\uD800-\uDBFF][\uDC00-\uDFFF])/g),
405+
result = matches === null;
406+
407+
if (!result) {
408+
validator.charErrorMessage = message.replace('{0}', matches.join());
409+
}
410+
411+
return result;
412+
}, function () {
413+
return this.charErrorMessage;
414+
}
415+
],
416+
399417
/* eslint-disable max-len */
400418
'email2': [
401419
function (value, element) {

0 commit comments

Comments
 (0)