Announcement Announcement Module
Collapse
No announcement yet.
one-to-many foreign key validation using onBindAndValidate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • one-to-many foreign key validation using onBindAndValidate

    Dear Springers,

    Apologies for the noob question but I have been struggling with this for a day and searching the forums & lists with no luck.

    Simple situation, you've seen it a million times.

    one-many relationship between objectA and objectB, lets say Building and Apartment.

    Apartment has a formController in which a list of Building populates a dropDown.

    selecting a Building to which Apartment belongs sets a string value buildingID in the request.

    The apartmentFormController (subclass of SimpleFormController) is trying to validate the data, and throws a bindException because String buildingID is not of type Building .

    So I override onBindAndValidate to handle the binding of buildingID to Apartment.Building .

    This works OK, but the BindException is still there, so my form never calls onSubmit and instead returns the page with the error message .

    How do I get rid of the error so that I can process my form submission ?


    OR ::: is there a better way to handle bindExceptions ? Do I need to write a custom validator for EVERY field that is a foreign key ?

    many thanks for your considerations

    busquelo

  • #2
    enclosed code sample

    maybe some code will help . am I doing something wrong here :
    Code:
     protected void onBindAndValidate(HttpServletRequest request,
                          Object command, BindException errors)
                   throws Exception {
            log.debug ("apartmentFormController::onbind, error count is: " + errors.getErrorCount());
            Apartment apt = (Apartment) command;
            String bldgId = request.getParameter("building");
            log.debug("bldgId is " + bldgId);
            apt.setBuilding(buildingManager.getBuilding(bldgId));
            log.debug ("set apt's building id, leaving method.");
                
    
        }

    Comment


    • #3
      Another approach that you might consider using is creating a custom PropertyEditor that will do the lookup for you. This is described in this thread:

      http://forum.springframework.org/viewtopic.php?t=299

      Comment


      • #4
        thank you very much, this solved my issue !

        Thanks, the custom PropertyEditor solved my issue !

        Cheeeeeeers !

        Comment

        Working...
        X