What is the best way to do validation in MVC 3? Here are the requirements:
Works client and server side.
Shares as much code between client and server as possible (attribute on model property seems ideal)
Works across async request
Display errors, validation messages, and success messages coming from the server side
Unobtrusive javascript, as minimal as possible
Dynamically added HTML should still validate the same way
My task this weekend is to build a robust solution for this, figured I'd ask here first before re-inventing or re-discovering the wheel with blood sweat and tears.
I would check out Brad Wilson's blog on this. He covers using unobtrusive validation in MVC3, sounds like exactly what you're looking for.
Adding more info per OP's comment
Regarding server side validation (custom validation), check out #jfar's response to a similar question I posted regarding custom validation -- he suggests that you should question your design if you're relying heavily on custom validation. In my case, I ended up going either with Ajax to handle my custom validation, or allowed the postback to perform the validation.
Related
We've an ASP.Net MVC2 web app (SQL 2008 in backend). We use Data Annotations at model level for all sprt of validations (hope its one of the best practices). So, our validations are performed on server side and errors (if any) are returned. Works fine.
Next, we've AJAX based postback jQuery plugin. In combination with MVC partial views, we've 'AJAXified' certain forms. I hope this is pretty much like a basic stuff with a little AJAX & jQuery.
Now, we want to bring the validation on client side as well (and still
persist the server side validation). We've found some simple &
basic way to have basic validations like required, format,
range, etc... using bassistance jQuery. But what about certain server
side validations, like duplication check, etc.. what are the best
practices?
Note that we've simplified our web app by not including the default Microsoft AJAX libraries. We prefer simplified jQuery plugins. Also Microsoft js files weight several KB. Though it might lesson the effort but it requires several files.(Data Annotations Validation + jQuery.Ajax Post)
For example, jquery forms plugin looks simpler then the default MicrosoftAjax.
jQuery plugins are abstract & self contained thats one reason we're away from the Microsoft scripts. Here're some options -
SOLUTION #1:
Using ASP.Net Data Annotations validations using pure jQuery, AJAX,
JSON & Partial views
SOLUTION #2:
ASP.NET MVC Client-Side Validation Summary with jQuery Validation Plugin
We need to keep things simple, clean and optimal. For example, this looks complex -
ASP.Net MVC: Can you use Data Annotations / Validation with an AJAX / jQuery call?
If possible we'd prefer to keep the validation in one place instead of having to replicate it in data annotations as well as in jQuery.
Thank you.
But what about certain server side validations, like duplication
check, etc.. what are the best practices?
In ASP.NET MVC 3 you could use the [Remote] data annotation.
In ASP.NET MVC 2 it doesn't exist but you could implement it using jQuery.validate remote rule. This assumes that you use the jQuery validate plugin of course instead of the built-in Microsoft client side validation framework.
Microsoft scripts are now obsolete. If you want to ease the migration towards ASP.NET MVC 3 and even 4 you should forget about those and use jQuery and jQuery validate which are the default client side frameworks now in ASP.NET MVC.
We're starting a new project, on the client side, we'll use a asp.net mvc3 application, accessing resources exposed by OpenRasta.
asp.net mvc has built in validation with Data Annotations, how usually people do validation with OpenRasta?
It depends the kind of validation you want. We don't currently support anything out of the box because the scenarios of what people wanted has always been very nebulous. People usually apply validation on the constructed model themselves rather than get OW to do it.
Happy to discuss your requirements and implement them as needed in 2.1, there's still a small open window for certain new features.
I recently try oforms module on orchard and I confess that I was expecting more.
My form not validating on IE8.
No ServerSide validation
No complex validation using Regex
Impossible to fill in dropdownlist options from a DB or an xml file
Impossible to set up workflows except e-mails
did I miss something that could change my opinion and make me buy this product ??
it does what it says it does. So it looks like a good module. Would be nice to have server side validation.
It works for me in IE8.
Is there a way to have the same kind of client-side validation in MVC 3 without being able to set the validation at the viewmodel level?
I have two requirements that are preventing me from adding my validation at the viewmodel level. I'd like to be able to have the same kind of "built-in" validation but without changing the viewmodel or controller code. I'd like to be able to change, turn on/off, validation at the view level post-deployment. Ideally I'd like to be able to edit the view mark-up without writing and maintaining mountains of my own JavaScript.
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
This may help ya, only validation on the client though so not really ideal.
You could create your own TypeDescriptionProvider which will allow to augment your viewmodel at runtime.
Based on this code sample (XmlMetadataTypeDescriptor, XmlMetadataTypeDescriptionProvider) you should be able to:
Augment viewmodel using external xml which contains validation attributes.
Turn on validation with
TypeDescriptor.AddProvider
and turn it off with:
TypeDescriptor.RemoveProvider
Update
For more information you can read great article: "Understanding the TypeDescriptor: A Metadata Engine for Designtime Code".
To make sure its a DRY approach all validation logic should of course go in the business logic (model).
How are validation messages presented to views, should be able to localize error messages
Can you generate javascript from the validation framework. Compatibility with JQuery would be perfect
Is the framework compatible with a DbC approach?
Edit:
I think this is the nicest one until now, Castle validator + live validation
http://blog.codeville.net/2008/04/30/model-based-client-side-validation-for-aspnet-mvc/
Validation Application Block
from Microsoft is one alternative but I have no experience with it