I want to have jQuery validation when the whole form is completed and submitted.
But when a drop down list is changed, I only want to have a POST to action so that I can get a refreshed view with updates. Say, when a state is chosen, the form post to the server side with current half-finished data, and the server returned the same page but with facilities specific to that selected state.
In this case, I want to submit the form without validation.
Like this:
$(document).on('change', '#selectStates', function(event) {
event.preventDefault();
var $form = $('#formEdit');
$form.bypassValidate();
$form.submit();
});
So what is the right way to $form.bypassValidate();
?
We know in submit buttons, we can add a formnovalidate
tag to bypass validation. But how can we do this in JavaScript?
Update with my solution
Okay, I looked through the post jQuery Validation plugin: disable validation for specified submit buttons again. And alas, there is a solution hidden in one of the answers.
formnovalidate
does not apply here if I need to bypass validation in JavaScript.
All we need is to call cancelSubmit
(which should be named cancelValidate
IMHO) on the validator
, which is bound to our jQuery form.
So my code on the select
input is:
$(document).on('change', '#selectStates', function(event) {
event.preventDefault();
var $form = $('#formEdit');
$form.data('validator').cancelSubmit = true;
$form.submit();
});