mirror of
https://github.com/yiisoft/yii2.git
synced 2025-10-30 18:17:00 +08:00
Fix #19254: Support specifying custom characters for yii.validation.trim() and replace deprecated jQuery.trim()
This commit is contained in:
@ -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);
|
||||
|
||||
Reference in New Issue
Block a user