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

cancel validation when submitting form via javascript #1513

Open
staabm opened this issue Jul 8, 2015 · 7 comments
Open

cancel validation when submitting form via javascript #1513

staabm opened this issue Jul 8, 2015 · 7 comments
Milestone

Comments

@staabm
Copy link
Member

staabm commented Jul 8, 2015

Inside a jquery-validated form I have a <a> link which is used submit the form and regenerate a captcha image.

The logic on the link is like onclick="$(this).closest("form").submit()".
In our forms the client side validation now kicks in and does not allow the form submit.

I need something like the formnovalidate or class="cancel" mechanism, but atm this features are only implemented on elemtens matching :submit. So this doesnt work for my <a>.
Since this is a very basic re-usable component I cannot "just change the markup or js".

https://github.com/jzaefferer/jquery-validation/blob/master/src/core.js#L27

@staabm
Copy link
Member Author

staabm commented Jul 8, 2015

I will try to workaround this problem by retrieving the form's validator and setting the internal cancelSubmit flag, but this feels is something I dont like that much ;).

per html-spec a formnovalidate is only supported on input elements, but what do you think of nevertheless supporting it on programmatically triggered submit()s @jzaefferer ?

@staabm
Copy link
Member Author

staabm commented Jul 9, 2015

in code, my current, ugly but working solution is:

$f = $("#myform");
$f.data('validator').cancelSubmit=true;
$f.submit();

@jzaefferer
Copy link
Collaborator

That doesn't look too terrible. I've looked into it, but couldn't come up with any reasonable generic solution. The only option I see is to document the cancelSubmit property.

Any (other) ideas?

@staabm
Copy link
Member Author

staabm commented Jul 9, 2015

Not sure whether documenting this property (and therefore promote it as public api) is a good thing.

Maybe we should instead add a method, to not mix up public api with a lot of different concepts (we already have a lot of ways of achieving things... options, api methods).

A submit() method would work like validator.submit();. A downside would be that there are already 2 sumbit methods (one on native dom FormElement and one on jQuery)

@jzaefferer
Copy link
Collaborator

You could hack jQuery's :submit selector to include your submit "button"... then you'd trigger the click() event before the submit() event.

@staabm
Copy link
Member Author

staabm commented Jul 21, 2015

After lookin at the problem from another angle, I guess we could (and maybe even should) check whether the event passed into delegate is flagged as isDefaultPrevented[1] and stop invoking our validation mechanics after all?

[1] http://api.jquery.com/event.isDefaultPrevented/

That way I could use a custom submit listener which is triggered before the jquery-validate and stop the validation there?

@jzaefferer
Copy link
Collaborator

That sounds good to me. The jQuery UI dialog widget does that, too, though to deal with key events.

@staabm staabm added this to the 2.0.0 milestone Feb 5, 2016
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

3 participants