Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I clear MVC client side validation errors when a cancel button is clicked when a user has invalidated a form?

I have a partial view that is rendered within a main view. The partial view takes advantage of System.ComponentModel.DataAnnotations and Html.EnableClientValidation().

A link is clicked, and div containing the partial view is displayed within a JQuery.Dialog().

I then click the save button without entering any text in my validated input field. This causes the client side validation to fire as expected, and display the '*required' message beside the invalid field.

When the cancel button is clicked, I want to reset the client side MVC validation back to it's default state and remove any messages, ready for when the user opens the dialog again. Is there a recommended way of doing this?

like image 529
Sci-fi Avatar asked May 09 '10 17:05

Sci-fi


People also ask

How remove validation message after field is valid in MVC?

So, to clear the validation errors, you can do something like this to a form : var fieldContexts = form. __MVC_FormValidation. fields; for(i = 0; i < fieldContexts.

How do I remove client-side validation?

To disable client-side validation, set the page's ClientTarget property to 'Downlevel' ('Uplevel' forces client-side validation). Alternatively, you can set an individual validator control's EnableClientScript property to 'false' to disable client-side validation for that specific control.

How do you remove data annotation validation on client-side?

To disable client side validation, we need to disable it by force. Notice the @data_val= “false”. It will disable the validation on this field.

How can disable validation on button click in ASP.NET MVC?

To disable validation when clicking a button, set the MVC Button. CausesValidation property value to false.


2 Answers

This answer is for MVC3. See comments below for help updating it to MVC 4 and 5

If you just want to clear the validation-messages so that they are not shown to the user you can do it with javascript like so:

function resetValidation() {         //Removes validation from input-fields         $('.input-validation-error').addClass('input-validation-valid');         $('.input-validation-error').removeClass('input-validation-error');         //Removes validation message after input-fields         $('.field-validation-error').addClass('field-validation-valid');         $('.field-validation-error').removeClass('field-validation-error');         //Removes validation summary          $('.validation-summary-errors').addClass('validation-summary-valid');         $('.validation-summary-errors').removeClass('validation-summary-errors');      } 

If you need the reset to only work in your popup you can do it like this:

function resetValidation() {         //Removes validation from input-fields         $('#POPUPID .input-validation-error').addClass('input-validation-valid');         $('#POPUPID .input-validation-error').removeClass('input-validation-error');         //Removes validation message after input-fields         $('#POPUPID .field-validation-error').addClass('field-validation-valid');         $('#POPUPID .field-validation-error').removeClass('field-validation-error');         //Removes validation summary          $('#POPUPID .validation-summary-errors').addClass('validation-summary-valid');         $('#POPUPID .validation-summary-errors').removeClass('validation-summary-errors');      } 

I hope this is the effect you seek.

like image 147
Falle1234 Avatar answered Oct 16 '22 11:10

Falle1234


If you are using unobtrusive validation that comes with MVC you can simply do:

$.fn.clearErrors = function () {     $(this).each(function() {         $(this).find(".field-validation-error").empty();         $(this).trigger('reset.unobtrusiveValidation');     }); }; 

------------------------------------------------------------------------

Third Party Edit: This mostly worked in my case, but I had to remove the $(this).find(".field-validation-error").empty(); line. This appeared to affect the re-showing of the validation messages when resubmitting.

I used the following:

$.fn.clearErrors = function () {         $(this).each(function() {             $(this).trigger('reset.unobtrusiveValidation');         });     }; 

and then called it like this:

$('#MyFormId input').clearErrors(); 
like image 32
Martin Avatar answered Oct 16 '22 09:10

Martin