What is triggering Unobtrusive Validation check in asp.net MVC 3 - asp.net-mvc-3

I'm trying to figure out how unobtrusive validation works in asp.net mvc 3.
I would like to know what is triggering the validation check when I click to submit the form. How is the script jquery.validate.unobtrusive.js bound to the form submit event ?
I also would like to know how can I manually prevent/trigger this check.

jquery.validate.unobtrusive is a validator for jquery.validate. It is like a extension.
jquery.validate.unobtrusive implements all the events and jquery.validate use it.
You can look into the jQuery.validate.js file and see that it uses the submit of the
form.
// validate the form on submit
this.submit( function( event ) {
...
If you want to trigger the validation by yourself you can call
$("#myform").valid()

Related

Invoking validation just after display the form JSF

I have a form where the user should be able to save even if there are validation errors.
These errors should be displayed once the user get into the form again.
So, i would like to invoke the validation of the entire form just after the form is displayed.
Currently, the validation is handled thru AJAX with several custom validators by using <f:validator/> and <p:ajax/> only when the user edits the field
Any suggestion is welcome!
Thanks!
An option would be to submit the form after it loads directly.
To achieve this you can use p:remoteCommand and something like jQuery('document').ready(function(){remoteCommandName()})

Validating form using jquery Ajax MVC

Up to this point, I’ve been using Microsoft Ajax for updates in my MVC app. I’m now switching to jQuery Ajax in order to standardize my approach.
However, I just realized that the required field in my model for the form did not fire and the user was able to submit the form even though some required fields were left blank.
What is the standard way to handle required fields if I’m handling my ajax functions using jQuery – i.e. I want the unobtrusive to handle my required fields by highlighting them and not allow the submit button to be clicked.
Also, to mention I am getting data-val attributes through the model.
P.S. I’m now using an event handler to detect click event on submit button for the form. In other words, I’m not using #using.Ajax.BeginForm to define my form.

MVC 4, Jquery Mobile, Ajax.BeginForm causing forms to submit twice

Does anyone know of any bugs or other insight to explain why when using MVC4 with jquery mobile, any form that is created using Ajax.BeginForm results in the form being submitted twice to the controller.
I had originally thought that since I am using the js bundling that the same js file might have been being included twice and might be triggering on form submit, however thats not the case, my list of js files are:
jquery-1.6.4.js
jquery-ui.1.8.11.js
jquery.mobile-1.1.0.js
jquery.unobtrusive-ajax.js
jquery.validate.js
knockout-2.0.0.js
MicrosoftAjax.js
MicrosoftMvcAjax.js
MicrosoftMvcValidation.js
Modernizr.js
Within the project Ajax is fully enabled for everything i.e. i'm not disabling anything through mobileinit.
I have not posted any form code because it literally happens with every form - a form with one field and a submit button will cause the submission twice - but only where Ajax.BeginForm is being used.
Html.BeginForm doesn't exhibit any of these problems.
I've been stuck on this for a few days now so any help would be greatly appreciated
jQuery Mobile by default hi-jacks any form submission and performs its own AJAX request. To stop this behavior you can place the data-ajax="false" attribute on any <form> tag. Also make sure to stop the regular behavior of the form so it doesn't submit normally, something like:
$('#my-form').on('submit', false);
Docs: http://jquerymobile.com/demos/1.1.0/docs/forms/forms-sample.html
You could also just use the built-in jQuery Mobile AJAX rather than including extra JS for it. Which would only require you to make the action attributes of the forms to the server-side file to which they posts.

MVC 3 - AutoPostback with CauseValidation=false Behavior

I'm familiar with the method of submitting a form via a change event like or a checkbox's check event. Most articles explain how to find the first form using jQuery and invoke it's submit() method. However, I wish to submit the form but not cause validation so the back-end can perform other actions. In my case, I have a dropdown that a users changes. Depending on the value, the back-end adds/removes certain controls. The issue I'm runing into is that the validation prevents the form from submitting.
Q: How do I submit the form so it does not perform client-side validation?
Remove the validation just before the form is submitted - check out the answers here How do I remove jQuery validation from a form?
I think you can just turn the validation off by calling #Html.EnableClientValidation(false)

ASP.NET MVC 3 jQuery client validation - disable for specific button (Ajax form)

How can I disable validation for one button inside the Ajax begin form?
Using jQuery validation I tried many ways from Google, but none of them worked (class="cancel", disableValidation attribute, etc.).
There may be other ways, but one for sure way is to remove all the validation rules if using unobtrusive validation via:
http://docs.jquery.com/Plugins/Validation/rules#.22remove.22rules
$('#frmMyName').validate().cancelSubmit=true;

Resources