Do you use MVC Unobtrusive Validation?
Updated by Brady Stroud [SSW] 1 year ago. See history
Validation rules can be added to a model object via Data Annotations or using the Fluent Validation API.
Fluent Validation is available as a Nuget package. See Do you use Fluent Validation?

Figure: OK Example - Data Annotation attributes decorate model properties to make them required

Figure: Better Example - Fluent Validation allows validation metadata to be added to a class without modifying the original class. This provides much more flexibility for code reuse
If you create a new MVC web application in VisualStudio 2013, unobtrusive validation will be enabled by default. Otherwise, it's simple to install from Nuget. To use it simply:
- Bind your razor views to model objects
- Use Html Helpers to render the form UI

✅ Figure: Good Example - this razor view binds to a strongly typed model object and uses HTML helpers.

Figure: the HTML UI rendered for this view now has data-validation attributes that are followed by JQuery validation to provide rich client-side validation.

Figure: On the server-side, the same validation rules will be checked when you call ModelState.IsValid
Categories
Need help?
SSW Consulting has over 30 years of experience developing awesome software solutions.