How to disable Validations when doing a Submit in the Dropdown Change event

Asked

Viewed 70 times

1

I have a modal with a dropdown, where, whenever an option is selected, a Ubmit is made and the model is sent to the action to be edited and then returned. The problem is that Validations get in the way, is it possible to disable validations just for my dropdown change event? The Save button should work normally, but for the dropdown it is necessary to deactivate them.

$('#pessoaNatureza').on('change', function (e) {    
  $(this).closest('form').submit();
}); 
  • You can pass a parameter executes validations and do within the Submit method a validation for if the execute validations is true execute, if it does not pass directly to the http request. $('#pessoaNatureza').on('change', function (e) { 
 $(this).closest('form').submit(false);
});

  • Only in all cases I have to give a Submit, only when it is by the change event, it does not validate the fields, that is, it must send the form without validating...

1 answer

0


By the plugin documentation, you can destroy the validator instance with the method:

validator.destroy();

Where validator is the name of the plugin instance you started. Example:

var validator = $("SELETOR DO FORMULÁRIO").validate();

Then your event change would be:

$('#pessoaNatureza').on('change', function (e) {    
   validator.destroy();
   $(this).closest('form').submit();
}); 

The validator will be deactivated before Submit and the form will be submitted in the event change dropdown.

Plugin documentation

Example:

var validator = $("#commentForm").validate();
$('#pessoaNatureza').on('change', function (e) {    
   validator.destroy();
   $(this).closest('form').submit();
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>

<form class="cmxform" id="commentForm" method="get" action="./">
   <p>
   <select id="pessoaNatureza">
      <option value="">Selecione...</option>
      <option value="1">1</option>
      <option value="2">2</option>
   </select>
   </p>
   <p>
   <input id="cname" name="name" minlength="2" type="text" required>
   </p>
   <button>Salvar</button>
</form>

  • Thanks @sam!!!! It worked 100% That’s what I was looking for and couldn’t find. You’re a beast!!!! :)

  • Just now I realized a detail: I’m using Identity and my login system is the one created automatically when creating the project. Your suggestion works perfectly, but if I’m logged in to the system no. Do you have any idea what this could be?

  • Later I’ll go to PC and we’ll see that blz

  • No trouble mate!!! When you can!

  • Any time you want to see it, just say the word.

  • I just saw your friend msg... I’m on...

  • Blz... come on. When you are logged in the code does not "destroy" the plugin? How is this?

  • Boy... it’s sinister... If I’m logged in, the plugin isn’t destroyed apparently. I have no idea what it could be....

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.