14

I have a form with a stage that has a dynamic number of groups of fields, where the number is based upon answers in the previous stage.

I'm generating the fields server-side as an array, i.e.

<input id="foo[0]"...
<input id="bar[0]"...

<input id="foo[1]"...
<input id="bar[1]"...

<input id="foo[2]"...
<input id="bar[2]"... etc

No matter the number, all fields are required & I also need to validate against type & number of digits in some cases. I'm using the jQuery validate plugin for client-side processing (yes, backed up with server-side stuff too) & the validation can't be done inline as the form needs to pass XHTML Strict (EDIT: see my addendum below).

My problem is that I can't work out how to use validate with a dynamic number of fields. Here's what the validate syntax typically looks like for the rest of the form:

$(document).ready(function() {

    // validate stage_form on keyup and submit
    var validator = $("#form_id").validate({

        // rules for field names
        rules: {

            name: "required", 
            address: "required",
            age: { required: true, number: true }

        },

        // inline error messages for fields above
        messages: {

            name: "Please enter your name", 
            address: "Please enter your address",
            age: { required: "Please enter your age", number: "Please enter a number" }

        }

    });

});

5 Answers 5

13

actually it should work If you'd use classes instead of initializing rules as validate() options.

Markup:

<input id="foo[0]" class="required"
<input id="bar[0]" class="required number"

<input id="foo[1]" class="required"
<input id="bar[1]" class="required email"

jQuery:

$(document).ready(function() {

  var validator = $("#form_id").validate({
    messages: {
            name: "Please enter your name", 
            address: "Please enter your address",
            age: { 
               required: "Please enter your age", 
               number: "Please enter a number" 
            }

    }

  });

});

hope this works. Sinan.

Sign up to request clarification or add additional context in comments.

5 Comments

That's what I ended up doing. See my self-answer.
And actually I'm going to accept your answer so the question gets closed. Cheers.
This makes :rules param useless, and the code less organized. Rules in html and message error in javascript snippet. Unfortunately, unique option.
The one downside of this is that it's very difficult to use more complex rules (e.g. depends) using classes.
See my answer for an alternate method that doesn't have any of these downsides.
2

Have you tried to use custom class rules to define the xhtml incompatible rules?

The example in the docs only uses one class, but I suppose you could combine different custom classes to achieve the validation rules you need. I haven't tried this for myself.

Comments

0

No answers so I'll post my "interim" solution, which is to set inline validation rules for 'required' and 'type', leaving 'maxlength' to server-side checking, then display custom messages with an inline title tag.

This is probably good enough for the purposes of this job, but I'm still curious if there's a way to do it 'completely' within jQuery.

Comments

0

Here is another way to do this.

/* Normal validate initialisation. */
$('#myForm').validate({

    /* Use the submitHandler method to add custom-selector-based validation. */
    submitHandler: function(form, ev) {

        /* Find your dynamic field/s. Note that you may want to access them via a scope external to validate, as any selection you do in this internal scope will be static from the form's pre-edit state. */
        var el = $('#selector');

        /* Do your custom validation. */
        if (  el.val() !== 'A'  ) {

            /* Show any errors:- 'fieldname': 'error message'. */
            this.showErrors({
                'name-of-a-field-near-where-you-want-your-error-placed': 'Please enter "A" to continue'
            });

            /* Prevent form submission. */
            return;
        } 
    }
});

1 Comment

One downside to this is that it doesn't run at the same time as any defined rules. So your user will get 2-stage validation unless you move all your rules into submitHandler.
-2

I found a way to do using "metadata".

This should be used inside a template with a dynamic name. So, i don't need to know the name.

The downside is still not possible to use the code with pure javascript with clean tags.

<script src="jquery.metadata.js" type="text/javascript"></script>

<input validate="{required: true, email: true, messages: { required: 'i'm required', 'i'm not valid e-mail' }}" name="dynamicRow[  myRandomNumber ]"type="text" class="input_normal" />

 $( function() {
     // setup stuff
     $.metadata.setType("attr", "validate"); 
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.