Announcement Announcement Module
Collapse
No announcement yet.
Validators for nested objects Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Validators for nested objects

    I know I've seen this addressed before, but I cannot find it for the life of me.

    I have an object that has a nested object that has a nested object. For example:

    Business->Location->Contact

    I am binding to the nested objects in my JSP using something like business.location.contact.name. That works great.

    The question I have has to do with the validators for my Location and Contact objects. How do I tell my form to use the LocationValidator and ContactValidator for the nested objects?

    Do I have to call them in my BusinessValidator object?

  • #2
    Location and Contact objects
    You can do type validation per class using a customer property editor. See section 3.13. Registering additional custom PropertyEditors of the reference docs.

    How do I tell my form to use the LocationValidator and ContactValidator for the nested objects
    AFAIK, you only have one command class which form parameters are bound to. This can be Business, Location or Contact, or a single object which can be an aggregation of all three, but I don't think you can bind to all three separately - so you only need one validator. You can set multiple validators (see org.springframework.web.servlet.mvc.BaseCommandCon troller), but they must support the same command class.

    Do I have to call them in my BusinessValidator object?
    You could do this, but are you actually binding new values to these parent objects, and so requiring validation?

    Comment


    • #3
      In my example, the command class is Business which has a Listing object. The Listing object in turn has a Contact object. So there is only 1 command class. Since Listing and Contact objects are contained by other "parent" objects I'm trying to figure out how to reuse 1 validate for the business objects.

      So, from your post, I would assume that I need to figure out how to create property editors for my containted objects so that Spring knows how to validate them. I've run across several postings about custom property editors but I never understood what they were really for. Does anybody know of a good tutorial on how to create and use them?

      Thanks

      Comment


      • #4
        Property Editors are for converting a String object to another class (e.g. "15/12/2004" to java.util.Date). When this fails, it is just type validation, and shouldn't be any cleverer than that. You can write custom ones, but it doesn't sound appropriate for you objects - it doesn't look like a single form field (i.e. a String) would be meaningful for a location Object that may be made up of several fields, so this is probably a red herring.

        However, in answer to:
        Does anybody know of a good tutorial on how to create and use them?
        There are some custom ones in Spring source. See org.springframework.beans.propertyeditors package.

        Having thought a bit more, probably my last question from my previous post is most relevant.
        ... are you actually binding new values to these parent objects, and so requiring validation?
        If not, just bind to the lowest nested object that is altered, and re-use it's validator. If you are altering parent values, I'd first look at whether I am altering the same values on the same objects from different forms. If this was necessary, then I'd call the other validator classes from the BusinessValidator. If not, I'd probably keep the validation all in the BusinessValidator. This would also cover the case where validation requires some combination of Business, Location and/or Contact details (e.g. Location is USA, but Contact is in Aus, generate error)

        Comment


        • #5
          Thanks. That's what I was hoping to hear. I found some information on property editors and have a better idea of how they work. Pretty cool, but as you guessed, not really what I need. Calling the validator of the nested object is really the best answer, I think.

          Comment


          • #6
            I have the same problem; to validate nested objects. How/where would I call a validator for the nested object?

            Comment


            • #7
              I would perhaps make the controller itself implement the Validator interface, setValidator(this), and in turn forward the validation from the controller's validate() method to the other (three or whatever number) validator classes.

              Comment


              • #8
                I would create three validators, one for Business, Location, and Contact. Assign the BusinessValidator as the validator for the form command.

                In the validate(...) method on BusinessValidator, invoke the Validator for Location (which could be set via dependency injection). And in the LocationValidator, explicitly invoke the ContactValidator.

                The key is to call the method Errors.pushNestedPath(String) before invoking the sub-validator and popNestedPath() afterwards. The sub validators don't need to know about the 'whole', they only need to know about the things they are validating. This allows you to use the same LocationValidator (or ContactValidator) on a standalone Location (Contact), if necessary.

                Here's the JavaDoc for the class: http://www.springframework.org/docs/...on/Errors.html

                And a related issue: http://opensource2.atlassian.com/pro...browse/SPR-186

                Regards,
                Christian

                Comment

                Working...
                X