« SharePoint 2010 - Search Error - "The search service is not able to connect to the machine that hosts the administration component" | Main | An Ode to LINQ »

07/13/2011

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

billy

Thanks for the post. So I have to create a ModelValidator and another Validator for my object I want validated? Webflow won't do this validation automatically like MVC does?

Jimmy Ray

The ModelValidator is simply a class that I used to aggregate calls. You don't need it. You don't need JSR-303 Javax Bean Validation either with SWF. You could go this route: http://static.springsource.org/spring-webflow/docs/2.0.x/reference/html/ch04s10.html

Or you can use JSR-303 like so: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/validation.html#validation-beanvalidation

Or this: http://static.springsource.org/spring/docs/3.0.0.RC3/spring-framework-reference/html/ch05s07.html

My approach lets you use JSR-303 constraint annotations, but also allows you to pick and use, based on view-state of your web flow, what constraints to validate.

billy

Do I need to have my model objects extend AbstractModel? When trying your example I get an error saying my model object is not type AbstractModel.

Jimmy Ray

yes, I have change the Customer type definition to read: public class Customer extends AbstractModel {

I do not supply this AbstractModel class, but you don't need my implementation to make this work. Worst case scenario, you could change generic type parameters in the ModelValidator.

billy

Yea thats what I ended up doing using a generic type. You've been very helpful but I have one more question. When stepping through the code I don't see how the valid property is ever false. In the ModelValidator it is set to true and then it is set to false inside the for loop of messages. messages is passed to the validateModelProperties method but in the CustomerValidator it is set as a new LinkedHashMap. What am I missing? It doesn't seem like the map will every have any records.

Jimmy Ray

The way we use it is that we pass model objects (Customer in this case) into the ModelValidator. The ModelValidator calls the JSR303 validator with this line: constraintViolations = validator.validateProperty(model, key);

If the validation fails: if (constraintViolations.size() > 0) {
isValid = false;

Then the isValid is marked to be true. If you create a Customer model instance and leave firstName null, you should see it return errors.

billy

Yea I understand that. The question was about the line above that. The for(String key : messages.keySet()). I just don't see how messages gets populated.

Jimmy Ray

messages is a map that is passed into the method. The messages map contains keys that are the strings ("firstName...") that match the bean properties that you are trying to validate. For each bean property, the ModelValidator calls validateProperty.

billy

Thanks again for the response. How does the messages map get populated in the CutomerValidator class? I see it declared like this Map> propertyMap = new LinkedHashMap>();
and then passed into the method.

Jimmy Ray

Put this (propertyMap.put("firstName", null);) in the CustomerValidator, before you call the ModelValidator.

billy

Got it! Thank you so much for helping me with this.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been saved. Comments are moderated and will not appear until approved by the author. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Comments are moderated, and will not appear until the author has approved them.

Contact Ironworks

  • Ironworks Consulting combines strategy, technology and design services to assist clients in the development of large-scale, complex technology projects. Ironworks offers three core services: Business & IT Alignment, Portal and Content Management, and Interactive.

    Contact us for help with your next project.

Ironworks is Hiring!

  • If you're the best... we want you to work here!

    If you're the best... we want you to work here!Ironworks is always on the lookout for experienced professionals who believe in hard work, having fun, and great client service.

    View our open positions, or, if you don't see an exact match, send us your resume.

Workswire e-Newsletter

  • Workswire e-newsletterSign up to receive Ironworks' quarterly e-newsletter for the latest news, events, client successes, industry insights and more!
    subscribe

About This Blog

Welcome to the Ironworks Technology Blog, Under the Hood. Ironworks has nominated solicited employee volunteers to write about various technology-related topics, from high-level application architecture all the way down to those pesky 1s and 0s that keep the world moving these days.


Continue reading »

Ironworks Twitter Updates

    follow me on Twitter

    Other Blogs We Love

    • TED | Talks | List
    • Information Architects
      We architect information.
    • John Resig - Blog
      John Resig is the Dean of Open Source and head of JavaScript development at Khan Academy and the author of the book Pro JavaScript Techniques. He’s also the creator and lead developer of the jQuery JavaScript library.
    • Iconify.it
      Welcome to Iconify.it – a blog about icons.
    • Garrett's Tech Musings
      Thoughts on technology, programming, and the industry
    • Fit and Finish
      Insight from the Ironworks User Experience Group
    • Fritillaria
      Information Architecture Across Dimensions
    • Amusingly MOSS
      ...It's funny how difficult some stuff is when it really shouldn't be
    • CAE Weblog
      Ramblings about the world of associations and their intersection with the Web.

    Become a Fan