mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 06:15:19 +08:00
Fixes #2756: Added support for injecting custom isEmpty
check for all validators
This commit is contained in:
@ -11,20 +11,20 @@
|
||||
*/
|
||||
|
||||
yii.validation = (function ($) {
|
||||
var isEmpty = function (value, trim) {
|
||||
return value === null || value === undefined || value == []
|
||||
|| value === '' || trim && $.trim(value) === '';
|
||||
};
|
||||
var pub = {
|
||||
isEmpty: function (value) {
|
||||
return value === null || value === undefined || value == [] || value === '';
|
||||
},
|
||||
|
||||
var addMessage = function (messages, message, value) {
|
||||
messages.push(message.replace(/\{value\}/g, value));
|
||||
};
|
||||
|
||||
return {
|
||||
addMessage: function (messages, message, value) {
|
||||
messages.push(message.replace(/\{value\}/g, value));
|
||||
},
|
||||
|
||||
required: function (value, messages, options) {
|
||||
var valid = false;
|
||||
if (options.requiredValue === undefined) {
|
||||
if (options.strict && value !== undefined || !options.strict && !isEmpty(value, true)) {
|
||||
var isString = typeof value == 'string' || value instanceof String;
|
||||
if (options.strict && value !== undefined || !options.strict && !pub.isEmpty(isString ? $.trim(value) : value)) {
|
||||
valid = true;
|
||||
}
|
||||
} else if (!options.strict && value == options.requiredValue || options.strict && value === options.requiredValue) {
|
||||
@ -32,85 +32,85 @@ yii.validation = (function ($) {
|
||||
}
|
||||
|
||||
if (!valid) {
|
||||
addMessage(messages, options.message, value);
|
||||
pub.addMessage(messages, options.message, value);
|
||||
}
|
||||
},
|
||||
|
||||
boolean: function (value, messages, options) {
|
||||
if (options.skipOnEmpty && isEmpty(value)) {
|
||||
if (options.skipOnEmpty && pub.isEmpty(value)) {
|
||||
return;
|
||||
}
|
||||
var valid = !options.strict && (value == options.trueValue || value == options.falseValue)
|
||||
|| options.strict && (value === options.trueValue || value === options.falseValue);
|
||||
|
||||
if (!valid) {
|
||||
addMessage(messages, options.message, value);
|
||||
pub.addMessage(messages, options.message, value);
|
||||
}
|
||||
},
|
||||
|
||||
string: function (value, messages, options) {
|
||||
if (options.skipOnEmpty && isEmpty(value)) {
|
||||
if (options.skipOnEmpty && pub.isEmpty(value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof value !== 'string') {
|
||||
addMessage(messages, options.message, value);
|
||||
pub.addMessage(messages, options.message, value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (options.min !== undefined && value.length < options.min) {
|
||||
addMessage(messages, options.tooShort, value);
|
||||
pub.addMessage(messages, options.tooShort, value);
|
||||
}
|
||||
if (options.max !== undefined && value.length > options.max) {
|
||||
addMessage(messages, options.tooLong, value);
|
||||
pub.addMessage(messages, options.tooLong, value);
|
||||
}
|
||||
if (options.is !== undefined && value.length != options.is) {
|
||||
addMessage(messages, options.is, value);
|
||||
pub.addMessage(messages, options.is, value);
|
||||
}
|
||||
},
|
||||
|
||||
number: function (value, messages, options) {
|
||||
if (options.skipOnEmpty && isEmpty(value)) {
|
||||
if (options.skipOnEmpty && pub.isEmpty(value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof value === 'string' && !value.match(options.pattern)) {
|
||||
addMessage(messages, options.message, value);
|
||||
pub.addMessage(messages, options.message, value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (options.min !== undefined && value < options.min) {
|
||||
addMessage(messages, options.tooSmall, value);
|
||||
pub.addMessage(messages, options.tooSmall, value);
|
||||
}
|
||||
if (options.max !== undefined && value > options.max) {
|
||||
addMessage(messages, options.tooBig, value);
|
||||
pub.addMessage(messages, options.tooBig, value);
|
||||
}
|
||||
},
|
||||
|
||||
range: function (value, messages, options) {
|
||||
if (options.skipOnEmpty && isEmpty(value)) {
|
||||
if (options.skipOnEmpty && pub.isEmpty(value)) {
|
||||
return;
|
||||
}
|
||||
var valid = !options.not && $.inArray(value, options.range) > -1
|
||||
|| options.not && $.inArray(value, options.range) == -1;
|
||||
|
||||
if (!valid) {
|
||||
addMessage(messages, options.message, value);
|
||||
pub.addMessage(messages, options.message, value);
|
||||
}
|
||||
},
|
||||
|
||||
regularExpression: function (value, messages, options) {
|
||||
if (options.skipOnEmpty && isEmpty(value)) {
|
||||
if (options.skipOnEmpty && pub.isEmpty(value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!options.not && !value.match(options.pattern) || options.not && value.match(options.pattern)) {
|
||||
addMessage(messages, options.message, value);
|
||||
pub.addMessage(messages, options.message, value);
|
||||
}
|
||||
},
|
||||
|
||||
email: function (value, messages, options) {
|
||||
if (options.skipOnEmpty && isEmpty(value)) {
|
||||
if (options.skipOnEmpty && pub.isEmpty(value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -127,12 +127,12 @@ yii.validation = (function ($) {
|
||||
}
|
||||
|
||||
if (!valid || !(value.match(options.pattern) || (options.allowName && value.match(options.fullPattern)))) {
|
||||
addMessage(messages, options.message, value);
|
||||
pub.addMessage(messages, options.message, value);
|
||||
}
|
||||
},
|
||||
|
||||
url: function (value, messages, options) {
|
||||
if (options.skipOnEmpty && isEmpty(value)) {
|
||||
if (options.skipOnEmpty && pub.isEmpty(value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -153,12 +153,12 @@ yii.validation = (function ($) {
|
||||
}
|
||||
|
||||
if (!valid || !value.match(options.pattern)) {
|
||||
addMessage(messages, options.message, value);
|
||||
pub.addMessage(messages, options.message, value);
|
||||
}
|
||||
},
|
||||
|
||||
captcha: function (value, messages, options) {
|
||||
if (options.skipOnEmpty && isEmpty(value)) {
|
||||
if (options.skipOnEmpty && pub.isEmpty(value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -174,12 +174,12 @@ yii.validation = (function ($) {
|
||||
h += v.charCodeAt(i);
|
||||
}
|
||||
if (h != hash) {
|
||||
addMessage(messages, options.message, value);
|
||||
pub.addMessage(messages, options.message, value);
|
||||
}
|
||||
},
|
||||
|
||||
compare: function (value, messages, options) {
|
||||
if (options.skipOnEmpty && isEmpty(value)) {
|
||||
if (options.skipOnEmpty && pub.isEmpty(value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -220,8 +220,9 @@ yii.validation = (function ($) {
|
||||
}
|
||||
|
||||
if (!valid) {
|
||||
addMessage(messages, options.message, value);
|
||||
pub.addMessage(messages, options.message, value);
|
||||
}
|
||||
}
|
||||
};
|
||||
return pub;
|
||||
})(jQuery);
|
||||
|
Reference in New Issue
Block a user