Reputation: 36242
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?
Upvotes: 0
Views: 308
Reputation: 31043
try
<script type="text/javascript">
$.validator.setDefaults({
ignore: ""
})
</script>
or you can consider making a custom validator.
THIS answer may help
Upvotes: 1