From e08222bebdec3d8dcab0be597d547fa24b3118aa Mon Sep 17 00:00:00 2001 From: Anton Date: Wed, 30 Mar 2022 23:46:08 +0300 Subject: [PATCH] Fix #19254: Support specifying custom characters for `yii.validation.trim()` and replace deprecated `jQuery.trim()` --- framework/CHANGELOG.md | 1 + framework/assets/yii.validation.js | 35 +++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 13aaaea1fc..ef0aa1915e 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -6,6 +6,7 @@ Yii Framework 2 Change Log - Bug #19243: Handle `finfo_open` for tar.xz as `application/octet-stream` on PHP 8.1 (longthanhtran) - Bug #19235: Fix return type compatibility of `yii\web\SessionIterator` class methods for PHP 8.1 (virtual-designer) +- Enh #19254: Support specifying custom characters for `yii.validation.trim()` and replace deprecated `jQuery.trim()` (WinterSilence) - Bug #19291: Reset errors and validators in `yii\base\Model::__clone()` (WinterSilence) - Enh #19308: Add `yii\web\UploadedFile::$fullPath` represents 'full_path' key added in PHP 8.1 (WinterSilence) - Bug #19303: Fix serialization in `yii\caching\Dependency::generateReusableHash()` (WinterSilence) diff --git a/framework/assets/yii.validation.js b/framework/assets/yii.validation.js index 2f1e06039b..c4185f9184 100644 --- a/framework/assets/yii.validation.js +++ b/framework/assets/yii.validation.js @@ -24,7 +24,7 @@ yii.validation = (function ($) { var valid = false; if (options.requiredValue === undefined) { var isString = typeof value == 'string' || value instanceof String; - if (options.strict && value !== undefined || !options.strict && !pub.isEmpty(isString ? $.trim(value) : value)) { + if (options.strict && value !== undefined || !options.strict && !pub.isEmpty(isString ? trimString(value) : value)) { valid = true; } } else if (!options.strict && value == options.requiredValue || options.strict && value === options.requiredValue) { @@ -243,8 +243,17 @@ yii.validation = (function ($) { } value = $input.val(); - if (!options.skipOnEmpty || !pub.isEmpty(value)) { - value = $.trim(value); + if ( + (!options.skipOnEmpty || !pub.isEmpty(value)) + && (!options.skipOnArray || !Array.isArray(value)) + ) { + if (Array.isArray(value)) { + for (var i = 0; i < value.length; i++) { + value[i] = trimString(value[i], options); + } + } else { + value = trimString(value, options); + } $input.val(value); } @@ -467,5 +476,25 @@ yii.validation = (function ($) { } } + /** + * PHP: `trim($path, ' /')`, JS: `yii.helpers.trim(path, {chars: ' /'})` + */ + function trimString(value, options = {skipOnEmpty: true, chars: null}) { + if (options.skipOnEmpty !== false && pub.isEmpty(value)) { + return value; + } + + value = new String(value); + if (options.chars || !String.prototype.trim) { + var chars = !options.chars + ? ' \\s\xA0' + : options.chars.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1'); + + return value.replace(new RegExp('^[' + chars + ']+|[' + chars + ']+$', 'g'), ''); + } + + return value.trim(); + } + return pub; })(jQuery);