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

Validation ignores elements outside <form> but with form attribute #2156

Open
robertmiles3 opened this issue Mar 27, 2018 · 15 comments
Open

Comments

@robertmiles3
Copy link

Subject of the issue

The plugin does not validate elements that are outside of the <form> but have the form attribute on them.

Your environment

  • version of jquery-validate: 1.17.0
  • which browser and its version: Chrome 65, macOS

Steps to reproduce

See https://jsfiddle.net/robertmiles3/hpundyj5/4/

Expected behaviour

On submit, validation should fail for both inputs.

Actual behaviour

On submit, validation only fails for the input inside the <form>.

@stale stale bot added the stale Used to mark stale issues label Jun 5, 2018
@robertmiles3
Copy link
Author

Still valid

@stale stale bot removed the stale Used to mark stale issues label Jun 5, 2018
@stale stale bot added the stale Used to mark stale issues label Aug 4, 2018
@jquery-validation jquery-validation deleted a comment from stale bot Aug 4, 2018
@stale stale bot removed the stale Used to mark stale issues label Aug 4, 2018
@jquery-validation jquery-validation deleted a comment from stale bot Aug 4, 2018
@Arkni
Copy link
Member

Arkni commented Aug 4, 2018

Hi there,

I don't really have the time for working on this issue. Would you or anyone like to add support for this? I'd be more than happy to walk you through the steps involved.

Thanks!

@SimonHeimberg
Copy link

SimonHeimberg commented Oct 26, 2018

not working

The form elements outside of the form

  • ❌ are not considered by $form.valid()
  • ❌ do not show any validation messages while editing
partial working
  • calling $element.valid()
    • ✔️ returns false when invalid
    • ✔️ displays the validation error message
    • ❌ does not delete the old validation error message
proceed?

Are there workarounds?
No idea where to start for fixing. Will probably have some time, but I need some hints.

SimonHeimberg added a commit to SimonHeimberg/jquery-validation that referenced this issue Oct 26, 2018
@SimonHeimberg
Copy link

I started on https://github.com/SimonHeimberg/jquery-validation/tree/relatedElementsOutsideForm, but will have to read styleguide, ...

SimonHeimberg added a commit to SimonHeimberg/jquery-validation that referenced this issue Oct 26, 2018
@staabm
Copy link
Member

staabm commented Oct 27, 2018

Dont worry about the styleguide. Just submit the PR, in case its working as expected we can fix the CS or at least provide you some guidance

SimonHeimberg added a commit to SimonHeimberg/jquery-validation that referenced this issue Oct 30, 2018
SimonHeimberg added a commit to SimonHeimberg/jquery-validation that referenced this issue Oct 30, 2018
SimonHeimberg added a commit to SimonHeimberg/jquery-validation that referenced this issue Oct 30, 2018
SimonHeimberg added a commit to SimonHeimberg/jquery-validation that referenced this issue Oct 30, 2018
SimonHeimberg added a commit to SimonHeimberg/jquery-validation that referenced this issue Oct 30, 2018
@SimonHeimberg
Copy link

pr is #2231
I need help for writing a test. And style maybe too.

@garima2094
Copy link

garima2094 commented Feb 11, 2019

The plugin does not validate elements that are outside of the <form> but have the form attribute on them.

This issue occurred because in the plugin we only find input fields which are the child of form but we did not get all fields which are outside the form but associated with the form.

after analyzing the code I found the solution, at line no. 646 Did small change and it works for me.

// Select all valid inputs inside the form (no submit or reset buttons) return $( this.currentForm ) .find( "input, select, textarea, [contenteditable]" )

Changed above code to this
// Select all valid inputs inside the form (no submit or reset buttons) return $( this.currentForm.elements ) .filter( "input, select, textarea, [contenteditable]" )

and also removed error context from the error function.
errors: function() {
var errorClass = this.settings.errorClass.split( " " ).join( "." );
return $( this.settings.errorElement + "." + errorClass, );
},

Then the plugin validates the input fields which are associated with form but present at outside the form.
Hope it will work for you!!

@staabm
Copy link
Member

staabm commented Feb 11, 2019

@garima2094 thx for this information. are you willing to provide a Pull Request and Unit Tests for this feature?

@garima2094
Copy link

garima2094 commented Feb 11, 2019

@staabm Yaa sure
here is the PR
#2261

garima2094 pushed a commit to garima2094/jquery-validation that referenced this issue Feb 12, 2019
@ACvijic
Copy link

ACvijic commented Jul 17, 2019

Any news on this?

@buzz100
Copy link

buzz100 commented Mar 4, 2020

Looks like issue is still active in the current release 1.19.1, is their any updates on this.
Thanks

@luis-fss
Copy link

I would like to inform you that I am also being affected by this issue.

@shashankdesigner
Copy link

shashankdesigner commented Feb 6, 2021

Hi
I would like to inform that this triggers same validation error message multiple times on same field.
In this case able to fix this with this extending mixin for validation and then passing errorNewContext as option.

errors: function () {
    var errorClass = this.settings.errorClass.replace(' ', '.');
    if(this.settings.errorNewContext) {
        return $.merge($(this.settings.errorElement + "." + errorClass, this.errorContext), 
                $(this.settings.errorElement + "." + errorClass, this.settings.errorNewContext));
    } else {
        return $(this.settings.errorElement + "." + errorClass, this.errorContext);
    }
}, 

@VMelnalksnis
Copy link

Seems like this issue is still relevant in 1.19.3. Both PRs mentioned in this issue seem to be left open with all checks passing and no unresolved comments, is there any plan on reviewing/merging them?

@luis-fss
Copy link

@vmtco can you check this, thank you.

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

No branches or pull requests