If i have [Required(AllowEmptyStrings = true)]
declaration in my view model the validation is always triggered on empty inputs. I found the article which explains why it happens. Do you know if there is a fix available? If not, how do you handle it?
相关问题
- Entity Framework throws exception - Network Relate
- Date with SimpleDateFormat in Java
- Slow loading first page - ASP.NET MVC
- RegEx Max and Min length characters for a Text Box
- How to validate Integer value in jquery
相关文章
- Angular Material Stepper causes mat-formfield to v
- “Dynamic operations can only be performed in homog
- Why doesn't Django enforce my unique_together
- Change color of bars depending on value in Highcha
- How to get server path of physical path ?
- X/Html Validator in PHP
- Using CascadeMode.StopOnFirstFailure on a validato
- DDD Domain Model Complex Validation
Note: I'm assuming you have AllowEmptyStrings = true because you're also using your view model outside of a web scenario; otherwise it doesn't seem like there's much of a point to having a Required attribute that allows empty strings in a web scenario.
There are three steps to handle this:
Step 1: The custom attribute adapter
I modified the RequiredAttributeAdapter to add in that logic:
Step 2. Register this in your global.asax / Application_Start()
Step 3. Override the jQuery "required" validation function
This is done using the jQuery.validator.addMethod() call, adding our custom logic and then calling the original function - you can read more about this approach here. If you are using this throughout your site, perhaps in a script file referenced from your _Layout.cshtml. Here's a sample script block you can drop in a page to test:
Rather than decorating the value with the 'Required' attribute, I use the following. I find it to be the simplest solution to this issue.