DISQUS

DISQUS Hello! Emad Ibrahim is using DISQUS, a powerful comment system, to manage its comments. Learn more.

Community Page

Jump to original thread »
Author

Client & Server Side Validation in ASP.NET MVC

Started by eibrahim · 9 ヶ月 ago

The asp.net mvc codeplex preview 5 version just came out a few days ago and it includes a lot nice enhancements.  For the best introduction read Scott’s post. 
Some of my favorite changes happened in validation.  Incidentally, I was already working o ... Continue reading »

25 comments

  • Emad - I think this stuff is looking promising. You have made the correct choice of inserting the JavaScript rules (as opposed to using classes to decorate the fields for two reasons I think:-

    CSS classes should be used for styling and grouping similar elements e.g. you could be using the class attribute already to style certain form elements or maybe using it to group specific elements which can then be manipulated with CSS or JavaScript jQuery etc. If you had used the class method for tagging the elements, you would have limited peoples use of them for other important reasons - so good choice :)

    We've rolled out a similar system (also based on Steve Sanderson's excellent work) and it's working well and is proving to be robust and extensible.

    Keep up the great work and looking forward to reading your book :)

    Mike
  • Thanks and glad to get some 3rd-party validation (no pun intended) for my design.
  • HI Emad ,

    Nice post and looking good.
    Which book your writing?
  • Thanks. The current title is "ASP.NET MVC Test Driven Development Problem Design Solution" and the publisher is Wrox.
  • Nice and helpful post. But I've one question, if I want to use livevalidation or other validation javascript then what should I do?
  • Check out setve's post and code, he uses livevalidation
    http://blog.codeville.net/2008/04/30/model-base...
  • Great code!

    Put this on CodePlex
  • I put it on google code because I wanted to try it out.. It's at http://code.google.com/p/mvcvalidation/
  • That is very nice... I implemented some of the livevalidation from Steve Sanderson. I like what you have done w/ it using Preview 5. I look forward to digging through your post in more detail!
  • Emad -- This is great! I really like how you integrated JQuery to do client-side validation.
  • I like this solution but it can't really validate some things, i.e. unique username..etc. It would be interesting to create an auto-ajax validator to go with asp.net mvc. In theory we could create a custom attribute that mapped a property to a an implementation of an interface and calls a HttpModule that knows how to look up that properties implementation of an interface and validate to see if the property is correct. Anyway...it might be way overkill, but I think this could be very useful in the long run.
  • Actually, it is pretty easy to do with jQuery's Validation plugin. One of
    the validators allows you to make a remote call... so you can easily call
    an action that checks if a username is unique and return a json result for
    the jquery script to parse... I haven't done it but check the plugin
    documentation for examples.
  • Nice work on this. I have just been digging into this plugin and the helper is a great idea.
  • Nice work, I like the optimisation of the writting of the client side validation.. but i do not like to be able to create an instance of the entities before validate with Castle.. I am trying to change this by a FormCollection...
  • I couldn't receive code from Google SVN repository! Anything wrong with SVN URL on Google Code?
  • It's working fine for me. Did you see the instructions at
    http://code.google.com/p/mvcvalidation/source/c...
  • It did not compile for me with MVC Beta1 - I posted an issue on this to the project.
    btw I'm looking at modifying it to use System.ComponentModel.DataAnnotations instead of castle as then I can put the validation attributes in a metadata class instead of the class to be validated. My classes are generated so putting attributes on the fields is not an option.
  • I think that code was written using MVC Preview 5. You will have to modify
    it to work with the Beta.
    I like using the DataAnnotations namespace instead and have switched to it
    in one of my projects.

    I use POCO classes for models but I write conversion functions that convert
    and from my POCO to my LINQ to SQL entities. Some grunt work but works
    well.
  • I have updated the source code to compile and work with MVC Beta 1. Get it from the google project page at http://code.google.com/p/mvcvalidation/
  • First off this is a great idea but It did not compile for me with MVC Beta1 - I posted an issue on this to the project.
    btw I'm looking at modifying it to use System.ComponentModel.DataAnnotations instead of castle as then I can put the validation attributes in a metadata class instead of the class to be validated. My classes are generated so putting attributes on the fields is not an option.
  • Hey Emad,

    Great work. I have a similiar issue to Jeremy's, but I am also doing what you are doing and abstracting the L2S data layer out and upkeeping my own model's. However I cannot set an attribute on the model to declare the validation rules as my model classes are being generated from CodeSmith.... yikes. What can I do so I can set this rules? Any ideas?
  • I am not sure what to do here. That is the problem with generated code and
    it is the same problem with using LINQ 2 SQL, you can't add attributes to
    the generated code.
    -Emad
  • Emad,

    I got past this problem by using System.ComponentModel.DataAnnotations, I am using a feature in Dynamic Data to achieve this (http://blogs.microsoft.co.il/blogs/noam/archive...) lucky find, but working well!

    I had to update my last posted example with a few bigger changes to support this. My last code still stands if you can add attributes to your model, as said I couldn't as mine was code generated. I have just outlined the approach I took in my blog (http://goneale.wordpress.com/2009/01/19/aspnet-...) rather than spamming yours. :)
  • Hi All,

    Emad, I hope you don't mind but I have proposed a change to your code at this point:

    signature declaration:

    public static string ClientSideValidation(this HtmlHelper htmlHelper,
    string formName,
    object modelToValidate,
    bool prefixWithModelName)


    and string builder:

    This caters for typical Model Binding scenarios where you include the model name as a prefix with a dot "." and as a jquery validator plugin requirement, we need double quotes " " as seen here: http://docs.jquery.com/Plugins/Validation/Refer...


    rules.AppendLine();
    messages.AppendLine();

    string field = (prefixWithModelName ? "\"" + modelToValidate.GetType().Name + "." + prop.Name + "\"" : prop.Name);
    rules.AppendFormat("{0}: {1}", field, "{");
    messages.AppendFormat("{0}: {1}", field, "{");

    rules.AppendLine();
    messages.AppendLine();


    I also made a change to leave fields as is without ToLower() to comply with my forms and class naming standard.
    I suppose to remove complexity entirely, all names could just be wrapped in double quotes though. Shrug.

    This is working well for me.
  • There is another validation toolkit called Validator Toolkit for ASP.NET on Codeplex.com. See http://www.codeplex.com/MvcValidatorToolkit

Add New Comment

Returning? Login