I am not sure about you, but I rarely, if ever, want to validate a form field that is hidden from view!
Consider the case where you have a several required fields on the screen and the user can take some action that causes some fields to then be hidden. Well, using the default unobtrusive jquery validation, it will still validate those fields. If you do not have a 'SummaryValidation', this means the form just will not submit - no errors or anything because the error is displayed on a hidden field!
How do we get it to stop doing this? For now, I know of only one way. You need to add one line of code into the jquery.validate.unobtrusive.js file.
Add line 97 as shown below (and of course the comma at the end of line 96):
Subscribe to:
Post Comments (Atom)
15 comments:
Nice! Did you submit this to Connect?
No, I did not...I guess I should...
This is definitely an oops on their part. I cannot imagine it's by design.
Thats a good solution, but...
how do u update the modelstate in the controller after u set the hidden value ???
This only stops the validation on the client side. The server side will still validate and modelstate will still show it.
I know... but imagine that my hidden field is the DateCreated.
in the Controller i will set the DateCreated. And the model now should be valid... how to do it ??
@Rafa - I am not sure what you are asking exactly.
Look into http://stackoverflow.com/questions/2623514/jquery-validate-ignore-elements-with-style. Seems to solve this issue.
yes, you can do it with jquery like is show at stack overflow, but making the above change will make it happen automatically with the unobtrusive validation.
Changing the jQuery code would invalidate your ability to include that file through a public CDN though.
I can not get this to work, does your input have style="visibility:hidden", will this work with Jquery.hide()?
Awesome. Works like a charm.
Sorry. But it doesn't work for me. I'm using this code to disable validation:
$('#Form').validate({
onsubmit: false,
onkeyup: false,
onfocusout: false,
onclick: false
});
I know this post is old, but Im having same issue and the image above is broken - please can you just post the line of code.
Thanks
Ian
Post a Comment