0

ASP.NET MVC 3 application.

I am trying to post form that has some dynamic fields which appear and disappear using JavaScript. I am using IValidatableObject for doing server side validation on some of these dynamic fields.

When these fields are visible and I post the form, validation works fine and it gives appropriate error message in the ValidationSummary and posts back. Now while these error messages are there and I hide these fields using

document.getElementById("schoolNotListed").style.display = "none";

and I try to post the form again, it pops up the ValidationSummary error each time. The control doesn't reach the controller neither does it reach the Validate() function in the viewmodel. It is some client side validation the is preventing form to post.

Is there a way I can skip or disable this Validation for these fields that are now hidden through javaScript. Or can I clear the ModelState through javaScript ?

If you have some suggestions as to how to approach it differently ?

Thanks

  • You can't clear the modestate through javascript. Can you pre-fill your fields out with dummy (valid) data when they are invisible? – Simon Whitehead Jul 26 '12 at 01:35

1 Answers1

0

The client side Javascript validation library builds up a rules object that does the validation

When you hide them, try removing the validator

$('#schoolNotListed').hide().rules('remove', 'required')
$.validate.unobtrusive.parse()

The second line tells the validator to rebuild the rules object.

This question might be useful: Disable required validation by JavaScript

Community
  • 1
  • 1
Andrew Burgess
  • 5,300
  • 5
  • 30
  • 37