0

So here's the problem: I have a form and a property adorned with [RequiredAttribute] altough initialy it's hidden on the form. There's a checkbox. When it's clicked it shows the element. So I wanted to validate the property only when it's not hidden, but on submit when it sends model to the controller I still see ModelState.IsValid == false (although element it's hidden) so how can I remove all the errors related to the property when the element is hidden (in javascript, before browser sends model to the controller)?

modifying $('form').validate().errorList not helping

Of course I can have a boolean flag (isHidden or something) and check the model manually on the controller based on that flag. But maybe there's easier way to modify validation properties on the client, maybe somehow they affect Model?

iLemming
  • 34,477
  • 60
  • 195
  • 309
  • 1
    Validation happens on the _server_. You need to modify your server-side validators. – SLaks Apr 12 '13 at 19:28
  • unobtrusive validation happens on the client though – iLemming Apr 12 '13 at 19:28
  • 1
    Unobtrusive validation does not affect server validation in any way. The code your trying to write can only work if there is a security hole. – SLaks Apr 12 '13 at 19:32

1 Answers1

1

try

<script type="text/javascript">
    $.validator.setDefaults({
        ignore: ""
    })
</script>

or you can consider making a custom validator.

THIS answer may help

Community
  • 1
  • 1
Rafay
  • 30,950
  • 5
  • 68
  • 101