cancel validation when submitting form via javascript
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
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 ?
in code, my current, ugly but working solution is:
$f = $("#myform");
$f.data('validator').cancelSubmit=true;
$f.submit();
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?
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)
You could hack jQuery's :submit selector to include your submit "button"... then you'd trigger the click() event before the submit() event.
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?
That sounds good to me. The jQuery UI dialog widget does that, too, though to deal with key events.