0

I have a form that is marked up something like this:

 <form>
    <label for="day">Day</label><input type="text" name="day" id="day">
    <label for="month">Month</label><input type="text" name="month" id="month">
    <label for="year">Year</label><input type="text" name="year" id="year">
 </form>

Unfortunately, the names on the fields are going to be dynamic and won't be known until runtime. There are 3 fields that are required and would like them to have 1 error message.

I tried to populate the groups using a variable called myGroupData, but it does not work. The code would look something like this:

$('#myform').validate({
  rules..., 
  groups myGroupData,
  errorPlacement
});

var myGroupData = {DateofBirth: "day month year"};
       or
var myGroupData = {DateofBirth: "field1 field2 field3 etc..."}'  //field1, field2, field3 will be the names of the dynamic fields

Thanks, any help would be appreciated.

Moxie C
  • 442
  • 1
  • 15
  • 32
  • Bottom line: After plugin initialization, you **cannot** _dynamically_ change any of the options, except for rules/messages. – Sparky Sep 30 '14 at 19:38

1 Answers1

0

As per the jsFiddle from your comment on my deleted answer:

You must define the variable before you use it...

var myGroups = {DateofBirth: "day month year"};

$('#myform').validate({
    rules: {
        day: "required",
        month: "required",
        year: "required"
    },
    groups: myGroups
});

DEMO: http://jsfiddle.net/v0dmuvor/

Not sure why I couldn't get this working initially... maybe jsFiddle flaked out.


NOTES:

  • .validate() is only the plugin's initialization method. Once called, it cannot (and will not) be called again. Subsequent calls are ignored.

  • The plugin author has not provided a method for dynamically changing any options except for rules/messages. Once .validate() is called, the plugin's options are locked in.


EDIT: Not sure if your idea of "dynamically" is the same as mine. However, if you're planning on changing the parameters of the groups option after the page has loaded and the plugin initialized, it cannot be done as per my notes above.

Sparky
  • 98,165
  • 25
  • 199
  • 285
  • I don't think this answers the question. This answer assumes that the coder knows the names of the fields but the question specifically states that he doesn't – they are only defined at run-time. That's what he means by dynamic... a bit like this question but for groups instead of individual fields: http://stackoverflow.com/questions/20561712/jquery-validate-plugin-on-dynamic-form-inputs-not-working – geoidesic Apr 04 '16 at 08:49
  • @geoidesic, regardless, in order to use the `groups` option without knowing the names before runtime, would still require that the `myGroups` variable be constructed at runtime as per the OP's example code. If that's not possible, then there is no solution, as this plugin provides no method(s) for changing these options after the plugin is initialized. – Sparky Apr 04 '16 at 22:13