refactor: 整合集控checkGroup radioGroup验证方法
This commit is contained in:
parent
2f5b0ba10a
commit
ee07113dfc
|
@ -176,13 +176,24 @@
|
|||
var $this = $(element);
|
||||
if ($this.is(':hidden')) return result;
|
||||
var methods = this.rules(element);
|
||||
var proxy = function(rule) {
|
||||
var proxy = function (rule) {
|
||||
if ($.isFunction($.validator.methods[rule])) {
|
||||
result = $.validator.methods[rule].call(this.options, $this.val(), element, methods[rule]);
|
||||
if (!result) {
|
||||
$this.attr('data-original-title', this.defaultMessage(element, { method: rule, parameters: methods[rule] }));
|
||||
return result;
|
||||
}
|
||||
|
||||
// checkGroup rule
|
||||
if (rule === 'checkGroup') {
|
||||
var $checkers = this.$element.find(this.options.childClass).filter(function () {
|
||||
var $this = $(this);
|
||||
return $this.hasClass(rule) || $this.attr(rule);
|
||||
});
|
||||
$checkers.removeClass(this.options.errorClass).removeClass(this.options.validClass);
|
||||
if (result) $checkers.tooltip('dispose');
|
||||
else $checkers.addClass(this.options.errorClass).tooltip();
|
||||
}
|
||||
}
|
||||
else {
|
||||
console.log('没有匹配的方法 ' + rule);
|
||||
|
@ -191,13 +202,13 @@
|
|||
}
|
||||
var remote = null;
|
||||
for (var rule in methods) {
|
||||
if(rule !== 'remote') {
|
||||
if (rule !== 'remote') {
|
||||
result = proxy.call(this, rule);
|
||||
if(!result) return false;
|
||||
if (!result) return false;
|
||||
}
|
||||
else remote = rule;
|
||||
}
|
||||
if(remote !== null) result = proxy.call(this, remote);
|
||||
if (remote !== null) result = proxy.call(this, remote);
|
||||
return result;
|
||||
};
|
||||
|
||||
|
@ -212,26 +223,21 @@
|
|||
return message;
|
||||
};
|
||||
|
||||
Validate.prototype.attributeRules = function (element) {
|
||||
var rules = {}, $element = $(element), value;
|
||||
Validate.prototype.attributeRules = function (element, rules) {
|
||||
var $element = $(element), value;
|
||||
|
||||
$.each(["radioGroup"], function () {
|
||||
value = element.getAttribute(this);
|
||||
if (value === "") value = true;
|
||||
value = !!value;
|
||||
rules[this] = value;
|
||||
if (value) {
|
||||
rules["required"] = false;
|
||||
$(element).on('change', ':radio', function () {
|
||||
$(this).trigger('input.lgb.validate');
|
||||
});
|
||||
$.each(["remote"], function () {
|
||||
var para = $element.attr(this);
|
||||
if (para) {
|
||||
if (element.name === "") element.name = element.id;
|
||||
rules[this] = $.formatUrl(para);
|
||||
}
|
||||
});
|
||||
$.each(["remote"], function () {
|
||||
if (element.getAttribute(this)) {
|
||||
if (element.name === "") element.name = element.id;
|
||||
var para = $(element).attr(this);
|
||||
rules[this] = $.formatUrl(para);
|
||||
|
||||
$.each(["radioGroup", "checkGroup"], function () {
|
||||
if (rules[this]) {
|
||||
delete rules.required;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
return rules;
|
||||
|
@ -240,7 +246,11 @@
|
|||
Validate.prototype.rules = function (element) {
|
||||
var $this = $(element);
|
||||
var rules = $this.data('lgb.Validate.Rules');
|
||||
if (!rules) $this.data('lgb.Validate.Rules', rules = $.validator.normalizeRules($.extend({ required: true }, $.validator.classRules(element), $.validator.attributeRules(element), this.attributeRules(element))));
|
||||
if (!rules) $this.data('lgb.Validate.Rules', rules = this.attributeRules(element, $.validator.normalizeRules($.extend(
|
||||
{ required: true },
|
||||
$.validator.classRules(element),
|
||||
$.validator.attributeRules(element)
|
||||
))));
|
||||
return rules;
|
||||
};
|
||||
|
||||
|
@ -275,6 +285,11 @@
|
|||
$.validator.addMethod("radioGroup", function (value, element) {
|
||||
return $(element).find(':checked').length === 1;
|
||||
}, "请选择一个选项");
|
||||
|
||||
$.validator.addMethod("checkGroup", function (value, element) {
|
||||
return $(element).parents('[data-toggle="LgbValidate"]').find(':checked').length >= 1;
|
||||
}, "请选择一个选项");
|
||||
|
||||
$.validator.addMethod("userName", function (value, element) {
|
||||
return this.optional(element) || /^[a-zA-Z0-9_@.]*$/.test(value);
|
||||
}, "登录名称不可以包含非法字符");
|
||||
|
|
Loading…
Reference in New Issue