admin管理员组文章数量:1279113
Whenever i use require_from_group
it disables all other validations. Any ideas why?
Also is there a way to group "Telefon" and "Mobitel" and apply require_from_group
to it?
$(document).ready(function(){
$("#fncMain").validate(
{
/*groups:{Call:"Telefon Mobitel"},*/
rules:{
Davcna:{required:true,exactlength:5, digits:true},
Idzav:{required:true,exactlength:5, digits:true},
Maticna:{required:true,exactlength:5, digits:true},
Telefon:{require_from_group: [1,".callme"]},
Mobitel:{require_from_group: [1,".callme"]}
},
messages:{
}}
);
});
All other fields not included here use simple "required" class. If i remove require_from_group
rules applied to "Telefon" and "Mobitel" all other field validations work fine.
Thanks for help.
EDIT html : (too long to post it here)
Whenever i use require_from_group
it disables all other validations. Any ideas why?
Also is there a way to group "Telefon" and "Mobitel" and apply require_from_group
to it?
$(document).ready(function(){
$("#fncMain").validate(
{
/*groups:{Call:"Telefon Mobitel"},*/
rules:{
Davcna:{required:true,exactlength:5, digits:true},
Idzav:{required:true,exactlength:5, digits:true},
Maticna:{required:true,exactlength:5, digits:true},
Telefon:{require_from_group: [1,".callme"]},
Mobitel:{require_from_group: [1,".callme"]}
},
messages:{
}}
);
});
All other fields not included here use simple "required" class. If i remove require_from_group
rules applied to "Telefon" and "Mobitel" all other field validations work fine.
Thanks for help.
EDIT html : http://cl.ly/29391q0Q3G231T2I380m (too long to post it here)
Share Improve this question edited May 8, 2012 at 8:59 John asked May 6, 2012 at 10:43 JohnJohn 1,6299 gold badges24 silver badges34 bronze badges 6-
1
Can you share the html for this form, at least as it relates to these rules? i.e. what things have the class
callme
, etc – Ryley Commented May 7, 2012 at 17:23 - @Ryley i posted my HTML (form part). Check the edit of my post. :) – John Commented May 8, 2012 at 9:00
- I think you've found a bug... – Ryley Commented May 8, 2012 at 15:27
- See this for details on the potential bug. If that ends up being the resolution, post it as an answer here and ping me for upvotes :) – Ryley Commented May 8, 2012 at 18:04
- Hehe i actually posted this bug report :) Thanks for simplifying it ;) – John Commented May 8, 2012 at 19:22
2 Answers
Reset to default 5@Tats_innit posted a custom require_from_group
here: https://stackoverflow./posts/13127475
Turns out, this also fixes a logged github bug that was released with version 1.10.0 of require_from_group
in additional-method-1.10.js
for jquery.validation
.
github issue: require_from_group disables other rules
smileyanp@github quoted this post in his solution where he reused @Tats_innit's function and created a test that showed it works correctly and doesn't disable validation on other rules defined prior to the require_from_group
.
This post is here as a time saver as this burned 3 hrs of googling for such a small detail..
FIX:
Just update additional-method-1.10.js
or execute this code after additional-method-1.10.js
has loaded (to overwrite the function).
jQuery.validator.addMethod("require_from_group", function(value, element, options) {
var numberRequired = options[0];
var selector = options[1];
var fields = $(selector, element.form);
var filled_fields = fields.filter(function() {
// it's more clear to pare with empty string
return $(this).val() != "";
});
var empty_fields = fields.not(filled_fields);
// we will mark only first empty field as invalid
if (filled_fields.length < numberRequired && empty_fields[0] == element) {
return false;
}
return true;
// {0} below is the 0th item in the options field
}, jQuery.format("Please fill out at least {0} of these fields."));
Edit: It actually looks like this fix has been incorporated into version 1.12.0 and you can find the CDN pointers for it here: http://jqueryvalidation/
And for reference:
http://ajax.aspnetcdn./ajax/jquery.validate/1.12.0/jquery.validate.js
http://ajax.aspnetcdn./ajax/jquery.validate/1.12.0/jquery.validate.min.js
http://ajax.aspnetcdn./ajax/jquery.validate/1.12.0/additional-methods.js
http://ajax.aspnetcdn./ajax/jquery.validate/1.12.0/additional-methods.min.js
I found this code below before I found the solution above, so my advice is to use the CDN links referenced above instead of pasting the code below into your JS file.
There is a better fix out on GitHub now (scroll to the very bottom), which I've copied here. This is not my work and the GitHub user sfreytag who wrote it, does not appear to be an SO contributor, I just wanted to get it into SO so other people who find this don't have to dig through threads on GitHub:
jQuery.validator.addMethod("require_from_group", function(value, element, options) {
var validator = this;
var selector = options[1];
var validOrNot = $(selector, element.form).filter(function() {
return validator.elementValue(this);
}).length >= options[0];
if(!$(element).data('being_validated')) {
var fields = $(selector, element.form);
fields.data('being_validated', true);
fields.valid();
$(element.form).valid();
fields.data('being_validated', false);
}
return validOrNot;
}, jQuery.format("Please fill at least {0} of these fields."));
I have done limited testing with this thus far, but it appears to be working as you'd expect, all validations occur (instead of blowing through any non "require_from_group" validations as before), so I'm happy with it so far. I just added it after the the validator declaration in the top of my JS code:
$.validator.setDefaults({
debug: true,
success: "valid"
});
jQuery.validator.addMethod("require_from_group", function(value, element, options) {
var validator = this;
var selector = options[1];
//continuation of code...
本文标签: javascriptJquery validate requirefromgroupStack Overflow
版权声明:本文标题:javascript - Jquery .validate require_from_group - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741251165a2365815.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论