Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Validate Plugin: How can I add groups to a validator after its been initialized?

On our site we are using an embedded sign-up form from our ESP that uses the jQuery validate plugin. We are customizing the form a bit, adding a couple of custom fields (first name, last name) and we want them to be grouped so there is only one error message for both the fields.

Since the form's validator has already been initialized I need to add in a few things dynamically. The plugin provides the rules( "add", rules ) method to add in validation rules dynamically, although we are just using class names to do this anyhow. But there is no clear way to set the groups option after the validator has been initialized.

I've tried a few different things to accomplish this, but none some to be working:

var settings = $("#mc-embedded-subscribe-form").validate().settings;
$("#mc-embedded-subscribe-form").validate($.extend(settings, {
    groups: {
        username: "FNAME LNAME"
    },
    errorPlacement: function(error, element) {
        if (element.attr("name") == "FNAME" || element.attr("name") == "LNAME") {
            error.insertAfter("#username_group");
        } else {
            error.insertAfter(element);
        }
    }
}));

The errorPlacement function is added to the validator, but not the groups.

I've also tried setting the groups option explicitly, but that has no effect either.

$("#mc-embedded-subscribe-form").validate().settings.groups = { username: "FNAME LNAME" };
$("#mc-embedded-subscribe-form").validate().groups = { username: "FNAME LNAME" };

I'm completely stumped as how to accomplish this.

like image 892
aaronp Avatar asked Jan 27 '10 21:01

aaronp


People also ask

Does jQuery validate require a form?

The jquery validate plugin requires a form element to function, so you should have your form fields (no matter how few) contained inside a form. You can tell the validation plugin not to operate on form submission, then manually validate the form when the correct submit button is clicked.

How do you check if jQuery validate is loaded?

if (typeof jQuery. fn. validationEngine === "function") { // It's loaded } else { // It's not. }

How can create custom validation rule in jQuery?

validator. addMethod("oneormorechecked", function(value, element) { return $('input[name="' + element.name + '"]:checked'). length > 0; }, "Atleast 1 must be selected"); And you can also override the message of a rule (ie: Atleast 1 must be selected) by using the syntax data-msg-rulename="my new message" .


1 Answers

I recently faced the same problem and found a different solution.

The Scenario

We have a table which grows dynamically as users add (or delete) rows. Each new row contains several elements, and we want each rows's input elements to be in a single validation group--one per row--because we only want one error label for each row. Because the rows are added dynamically--well after we call $('#the-form').validate()--we needed a way to add a new group each time the user adds a row.

Our Solution

We hacked the validator object by directly modifying its groups member:

// on document ready:
validator = $('#the-form').validate({
    groups: ...,
    rules: ...,
    messages: ...,
    etc.
});

...

// later, when we need to add a new validation group:
validator.groups['first_name_row_5'] = 'full_name';
validator.groups['last_name_row_5'] = 'full_name';

Those last two lines are equivalent to

groups: {full_name: 'first_name_row5 last_name_row_5'}

in the validator options, but can be added after the initial call to validate().

It's a hack into the internals of jquery.validate, but it works (with jquery validate v1.9.0).


Finally, to directly answer the OP's question: instead of this:

$("#mc-embedded-subscribe-form").validate().groups = { username: "FNAME LNAME" };

try this:

var validator = $("#mc-embedded-subscribe-form").validate();
validator.groups['FNAME'] = 'username';
validator.groups['LNAME'] = 'username';
like image 71
ron rothman Avatar answered Sep 27 '22 16:09

ron rothman