1

How do I add groups: { } for require_from_group added in addClassRules.

$.validator.addClassRules("group_input", {
        require_from_group: [1,".group_input"]
    });

Since I don't want to give name in rules as names are dynamically generated I have given validation using class. How do I add groups because I get error messages for every text field.

Thanks in advance.

Sparky
  • 98,165
  • 25
  • 199
  • 285
User
  • 85
  • 2
  • 11
  • When you say _"names are dynamically generated"_, what exactly do you mean? Are they generated on the server (by PHP, etc.) when the page is constructed OR are they generated after the page was constructed using jQuery DOM manipulation techniques? – Sparky Feb 02 '15 at 16:35

1 Answers1

0

You cannot put the groups option into the .addClassRules() method.

The groups option is something you can only put inside of the .validate() method. You must reference the fields by their name attributes.

$('#myform').validate({
    // other options,
    groups: {
        myGroup: "field1name field2name field3"
    }
});

However, if you have a large number of fields, or in your case, the name is dynamically generated, you can construct the list of names external to .validate() and simply use a variable in place of the list.

var names = "";                          // create empty string
$('.group_input').each(function() {      // grab each input starting w/ the class
    names += $(this).attr('name') + " "; // append each name + single space to string
});
names = $.trim(names);                   // remove the empty space from the end

$('#myform').validate({
    // other options,
    groups: {
        myGroup: names  // reference the string
    }
});

Working DEMO: http://jsfiddle.net/e99rycac/

Source: https://stackoverflow.com/a/28029469/594235

Community
  • 1
  • 1
Sparky
  • 98,165
  • 25
  • 199
  • 285