# Thursday, March 24, 2011
« How Data Annotation Validators work on e... | Main | Can’t use SetEntitySetPageSize and MaxRe... »

One of the awesome features in MVC is Unobtrusive JavaScript.  In MVC 3 apps you can set this in the web.config.  It’s on by default.

I have a legacy database that has a field that is required and doesn’t allow nulls but the application allows for empty strings to be entered into the field.  I know, what’s the point of requiring a field and allowing empty strings (if empty string actually means something, OK). 

When I set the Required Data Annotation with the AllowEmptyString=true on the field the validator was still returning an error.  On a hunch I set UnobtrusiveJavaScriptEnabled=false and then field began working as expected.

What gives?

MVC 3 uses jQuery validators when UnobtrusiveJavaScriptEnabled is true.  For the Required annotation it uses the jQuery required validator which doesn’t have the AllowEmptyString parameter or one like it.  Essentially, the validator ignores this parameter. Thanks to @srkirkland for pointing this out. And he would know, he runs the OSS Data Annotations Extensions project.

Happy Coding!

Friday, July 15, 2011 3:04:16 AM (US Mountain Standard Time, UTC-07:00)
See this solution on StackOverflow: http://stackoverflow.com/questions/6437150/requiredattribute-with-allowemptystring-true-in-asp-net-mvc-3-unobtrusive-validat
Friday, July 15, 2011 7:05:36 AM (US Mountain Standard Time, UTC-07:00)
Great solution Jon!
Wednesday, March 17, 2021 12:05:31 PM (US Mountain Standard Time, UTC-07:00)
farmacia morlan http://essinreceta.com/ viagra generico
Please login with either your OpenID above, or your details below.
(will show your gravatar icon)
Home page

Comment (Some html is allowed: b, blockquote@cite, em, i, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

[Captcha]Enter the code shown (prevents robots):

Live Comment Preview