Announcement Announcement Module
Collapse
No announcement yet.
Validation errors with very complex objects Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Validation errors with very complex objects

    Hi all,

    I am running into a situation that must be common with Spring MVC, but I can't seem to find any previous posts covering it.

    I have a domain object that contains a map. I am using this domain object as a form backing bean and would like to be able to manipulate the objects in the hash via a form. This is no problem, I can retrieve the objects from the map in my view and bind to them with code like:

    Code:
    <#if object.objMap?keys?seq_contains(foo)>	
    	<fieldset class="displayObjectValues">
    	<@spring.formInput "object.objMap[foo].message1"/>
                  <@spring.bind"object.objMap[foo].message1"/>${spring.status.errorMessage}</p>
    ......
    Easy! I can pull the objects out of the map, by key, because freemarker is very powerful.

    The problem is with validation. I can pull the objects out of the map using standard Java syntax, I can run my checks on them, but I can't specify the "field" in my rejectValue calls.

    This code should make it clearer:

    Code:
    fooObject = (FooObject)obj.getObjMap.get("foo");
    
    if (foo.getMessage1() < MIN_MESSAGE_LENGTH) {
    	
            err.rejectValue("???????", "otpLengthTooShort", 
    			"You cannot generate messages that are less than " 
    					+ MIN_MESSAGE_LENGTH + " characters long");
    }
    What do I put instead of ??????? ? How do I reference this object that was pulled out the map? What is it's fieldname?

    My problem (as you can see) is that this isn't a simple object1.object2.field case. Because I am using a map to store objects I can't use the standard . notation.

    Is there some notation that I can use in the rejectValue call for the field that will allow me to bind an error to a particular field, from a given key in the map? Alternatively, is there some trick that I am missing?

    Thanks for your help with this,

    Cheers,
    Deakin

  • #2
    Isn't it always the way?

    Just after you post a cry for help, you sort it out. It actually turned out to be very simple.

    For the example in my post above, the field name for rejectValue is:

    objMap[foo].message1

    So my rejectValue looks like this:

    Code:
    fooObject = (FooObject)obj.getObjMap.get("foo");
    
    if (foo.getMessage1() < MIN_MESSAGE_LENGTH) {
    	
            err.rejectValue("objMap[foo].message1", "otpLengthTooShort", 
    			"You cannot generate messages that are less than " 
    					+ MIN_MESSAGE_LENGTH + " characters long");
    }
    I figured it wouldn't be too hard.

    Cheers,
    Deakin.

    Comment

    Working...
    X