Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting Fields With Same Name #423

Closed
benlilley opened this issue May 14, 2012 · 7 comments
Closed

Supporting Fields With Same Name #423

benlilley opened this issue May 14, 2012 · 7 comments

Comments

@benlilley
Copy link

First thank you for all your work on this plugin, it has made my life easier on many occasions. The reason I'm submitting this issue is because today I ran into an issue where I needed to validated fields with the same name, something like: <input type="text" name="foo[]" value="" /> - the problem is the plugin will only validate the first field with this name which I'm sure you know.

I spent a while looking for solutions but couldn't find any that worked correctly until I stumbled on this. This works perfectly! The general idea that you update the checkForm function to this:

checkForm: function() {
            this.prepareForm();
            for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
                if (this.findByName( elements[i].name ).length != undefined && this.findByName( elements[i].name ).length > 1) {
                    for (var cnt = 0; cnt < this.findByName( elements[i].name ).length; cnt++) {
                        this.check( this.findByName( elements[i].name )[cnt] );
                    }
                } else {
                    this.check( elements[i] );
                }
            }
            return this.valid();
        },

Is this a viable solution? If so is it possible to get this implemented into the core to solve issues in the future?

@vaibhav011286
Copy link

This works fine but thers's a problem with this solution that it also adds validation to disabled elements which were working as default.

Please, try to solve this problem also.

@dsand1234
Copy link

Why not override the elements function, since it is what stops at the first name:

$.validator.prototype.elements = function() {
    var validator = this;
    // select all valid inputs inside the form (no submit or reset buttons)
    return $(this.currentForm)
        .find("input")
        .not(":submit, :reset, :image, [disabled]")
        .not( this.settings.ignore )
        .filter(function() {
            !this.name && validator.settings.debug && window.console && console.error( "%o has no name assigned", this);
            return validator.objectLength($(this).rules());
        });
};

@gustavohenke
Copy link

It's worth to note, @dsand1234, that find("input") should be find(":input").

@pierrybos
Copy link

This solution the problem, i temporarily implement the code before $(form).validate() call. But still a problem: the error message is written on first occurrence.

@adeshpandey
Copy link

This works for me but error message is displayed after first input. Can anybody suggest any solution to place error messages ?

@staabm
Copy link
Member

staabm commented Apr 24, 2014

need also support for fields with same name...

paambaati added a commit to paambaati/nodequiz that referenced this issue Apr 28, 2014
…es is solved. PREVIOUSLY UNNOTICED PROBLEM - validations don't seem to be cloned. Known issue - see jquery-validation/jquery-validation#423
@jzaefferer
Copy link
Collaborator

I'm sorry for the lack of activity on this issue. Instead of leaving it open any longer, I decided to close old issues without trying to address them, to longer give the false impression that it will get addressed eventually.

To the reporter (or anyone else interested in this issue): If you're affected by the same issue, consider opening a new issue, with a testpage that shows the issue. Even better, try to fix the issue yourself, and improve the project by sending a pull request. This may seem daunting at first, but you'll likely learn some useful skills that you can apply elsewhere as well. And you can help keep this project alive. We've documented how to do these things, too. A patch is worth a thousand issues!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants