Mvc3 validation function to run after verification completes

I use MVC3 client side validation. Is there a way to run a function once after the validators are started?

Ideally, I want settngs.errorPlacement to trigger and put my errors accordingly, and once that is complete, I want the other function to be run only once. At the moment, I am calling the function from within settngs.errorPlacement and starting for every error that has a performance hit.

Any help gratefully received.

Update

When an unobtrusive client-side check is triggered in MVC, each validator will call either successful or the errorPlacement function. Basically, if I have fifteen validators on the page, one of these functions will be performed for each validator (i.e. 7 successful, 8 errors or 5 successes and 10 errors). What I want to do is call a function that will be run once after the testers are looped, and the success and errorPlacement functions executed bits there ...

I call the function 15 times at the moment, because I can’t figure out where I can put the function, where it will run only once.

+4
source share
4 answers

The jQuery validation plugin intercepts a submit form event with an event handler that calls the validate() function to validate() form before submitting, and thus prevents the form from submitting if validation fails.

If you hook up the submit event for each of your forms using jQuery and do it at the bottom of the form, it will add your own handler to execute before the plugin handler. Thus, you can call the valid() function on the form (s) yourself and run any custom code you want after a full form check. The plugin valid() function calls the validate() function on the form, and then returns a boolean for success / failure. If you don't care about success / failure, you can just call validate() directly and not call valid() .

When you call valid() or validate() on the form, it will continue to work with the unobtrusive validation of the MVC3 client to mark the form with validation errors, except that now you can do something else after the call.

I set breakpoints in my own submit() handler (below) and in the jQuery Validation validate() and valid() plugin functions. I see that they get called once with this implementation, so your validation for the full form should not be done multiple times.

Of course, the plugin also intercepts tricks, blur, keystrokes and event clicks, so the validators for individual fields are already called several times by design, so the user is provided with immediate feedback on fields with an error / allowed form. Validation is lazy because it does not run until the user enters a value in the field. When the user enters a value and leaves the field, this field is checked. If the field is marked invalid, a re-check is performed because the invalid field is checked repeatedly at various events until it is recognized as valid.

At the bottom of the page, place the tags below the form . As written, with the $("form") selector, it handles the submit event for all form elements on the page. Of course, you can use the jQuery selector to select specific form tags for validation. Just call valid() or validate() on the form whenever you want to perform validation, and follow it with any code that you want to execute once.

To get the test result:

 <form> ... </form> <script type="text/javascript"> $("form").submit(function (e) { if ($(this).valid() == false) { alert("yo!"); // execute anything you want following validation } }); </script> 

Or, if you do not want to know the result of the check:

 <script type="text/javascript"> $("form").submit(function (e) { $(this).validate(); alert("yo!"); // execute anything you want following validation }); </script> 
+4
source

They do not have the ability to do this, but you can do this by overriding the existing form function or something like that.

 var originalForm = $.validator.prototype.form; $.validator.prototype.form = function() { var returnValue = originalForm.call(this); if(!returnValue) { //do my custom invalid handling here } return returnValue; } 

I have not tested this code, I will try to expose the fiddle, but I am not 100% sure that the unobtrusive implementation looks like in MVC3, since I have not used it yet. If you could put a fiddle with the layout of your HTML / JS output, we could figure it out.

+1
source

I would suggest replacing the defaultShowErrors function in jquery.validate.js, mirror it, and add a call to your layout function. defaultShowErrors is called after the validation has been run, whether for a single element or the entire form.

Add the following to the javascript file (inserted after jquery.validate.js) or to the script in your page:

 $.extend($.validator.prototype, { defaultShowErrors: function() { for ( var i = 0; this.errorList[i]; i++ ) { var error = this.errorList[i]; this.settings.highlight && this.settings.highlight.call( this, error.element, this.settings.errorClass, this.settings.validClass ); this.showLabel( error.element, error.message ); } if( this.errorList.length ) { this.toShow = this.toShow.add( this.containers ); } if (this.settings.success) { for ( var i = 0; this.successList[i]; i++ ) { this.showLabel( this.successList[i] ); } } if (this.settings.unhighlight) { for ( var i = 0, elements = this.validElements(); elements[i]; i++ ) { this.settings.unhighlight.call( this, elements[i], this.settings.errorClass, this.settings.validClass ); } } this.toHide = this.toHide.not( this.toShow ); this.hideErrors(); this.addWrapper( this.toShow ).show(); // INSERT CALL TO YOUR CUSTOM PLACEMENT FUNCTION HERE } }); 
+1
source

place [SessionState(SessionStateBehavior.Default)] over the class definition, and then check if (Session.IsNewSession == true) so that it only starts for the first time. alternatively, you can set Session["somevariable"] = "somevalue" when you want to interrupt your function and check if (Session["somevariable"] == "somevalue") . this way you can switch efficiently if your function should be excluded.

-1
source

Source: https://habr.com/ru/post/1380369/


All Articles