I think that I've came accross a bug in the jQuery Validate Plugin about the groups setting.
Here is the initialisation example found on
jquery.com :
- $("#myForm").validate({
- groups:{
- groupname1: "inputname1 inputname2",
- groupname2: "inputname3 inputname4"
- }
- });
Based on this example I came up with a fonction that filled the setting groups dynamically. My groups of input are wrapped in divs with class="group".
- $("#myForm").validate({
- groups: getGroups();
- });
- function getGroups(){
- var result = {};
- $('#myForm .group').each(function(i) {
- var names = "";
- $(this).find('input').each(function(){
- names += $(this).attr('name') + " ";
- });
- result['groupname' + i] = names;
- });
- return result;
- }
There is no problem with that in the first place.
Now lets say you dynamically add some inputs to the form using jquery and you want to add them to a group in the validator..
If the validator has already been initialised you won't be able to add another group using the same method.
I've tried all of this:
- $("#myForm").validate({
- groups: getGroups();
- });
=> Do nothing
- $("#myForm").validate().setting.groups = getGroups();
=> Do nothing
- $("#myForm").validate().groups = getGroups();
=> Break the first groups created during the initialization !!
So we can break groups!! That's a good start...
So I checked the structure of '$("#myForm").validate().groups' and noticed that it has not the same as when I initialized it.
After initialization the structure looks like this:
- groups:{
- inputname1:"groupname1",
- inputname2:"groupname1",
- inputname3:"groupname2",
- inputname4:"groupname2",
- }
So I ended up with this solution:
- // Initialization
- $("#myForm").validate();
- $("#myForm").validate().groups = getGroups();
- // getGroups Function
- function getGroups(){
- var result = {};
- $('#myForm .group').each(function(i) {
- $(this).find('input').each(function(){
- result[$(this).attr('name')] = 'groupname' + i;
- });
- });
- return result;
- }
I can now call the getGroups() whenever I want =)
Is it supposed to be the normal behavior ? Am I doing something wrong ?