Skip to content

Commit b82d8cc

Browse files
IamDavidovichjzaefferer
authored andcommitted
Allow groups to be an array or a string - Fixes jquery-validation#479
Field names can contain spaces (as they are a CDATA type). This causes problems with groups, as the string listing the fields is split on spaces. This commit allows the groups option to be either a space-separated string or an array of strings so that field names with spaces can be added. It also adds a unit test to ensure that both string and array based groups are processed into the same data structures.
1 parent 696452f commit b82d8cc

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

jquery.validate.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,10 @@ $.extend($.validator, {
307307

308308
var groups = (this.groups = {});
309309
$.each(this.settings.groups, function(key, value) {
310-
$.each(value.split(/\s/), function(index, name) {
310+
if (typeof value === "string") {
311+
value = value.split(/\s/);
312+
}
313+
$.each(value, function(index, name) {
311314
groups[name] = key;
312315
});
313316
});

test/test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,22 @@ test("ajaxSubmit", function() {
796796
jQuery("#signupForm").triggerHandler("submit");
797797
});
798798

799+
test("validating groups settings parameter", function() {
800+
var form = $("<form>");
801+
var validate = form.validate({
802+
groups: {
803+
arrayGroup: ["input one", "input-two", "input three"],
804+
stringGroup: "input-four input-five input-six"
805+
},
806+
});
807+
equal(validate.groups["input one"], "arrayGroup");
808+
equal(validate.groups["input-two"], "arrayGroup");
809+
equal(validate.groups["input three"], "arrayGroup");
810+
equal(validate.groups["input-four"], "stringGroup");
811+
equal(validate.groups["input-five"], "stringGroup");
812+
equal(validate.groups["input-six"], "stringGroup");
813+
});
814+
799815

800816
module("misc");
801817

0 commit comments

Comments
 (0)