jquery-validation icon indicating copy to clipboard operation
jquery-validation copied to clipboard

cancel validation when submitting form via javascript

Open staabm opened this issue 10 years ago • 7 comments

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 avatar Jul 08 '15 10:07 staabm

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 avatar Jul 08 '15 10:07 staabm

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

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

staabm avatar Jul 09 '15 12:07 staabm

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?

jzaefferer avatar Jul 09 '15 19:07 jzaefferer

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)

staabm avatar Jul 09 '15 20:07 staabm

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

jzaefferer avatar Jul 20 '15 15:07 jzaefferer

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?

staabm avatar Jul 21 '15 09:07 staabm

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

jzaefferer avatar Jul 21 '15 10:07 jzaefferer