1 Reply Latest reply on May 26, 2008 12:36 AM by Ronald van Kuijk

    Compound form fields

    Matt Davis Newbie

      Was hoping someone could give me some tips on how to implement some moderately complex form validation using Hibernate annotations in a seam entity.


      One simple example would be a zip code... say I wanted to have my form have 2 fields for zip code, one for the initial 5 numbers and one for the extended 4 digits.


      My first shot looks like this:


                  <s:decorate id="zip1Decoration" template="layout/edit.xhtml">
                      <ui:define name="label">Zip Code</ui:define>
                      <h:inputText id="zip1"
                                    required="true"
                                   value="#{Address.zip1}"/> - 
                      <h:inputText id="zip2"
                                    required="false"
                                   value="#{Address.zip2}"/>
                  </s:decorate>
      




      And then I've got the entity bean fields:


           @Column(name="zip1")
           @Length(min=5, max=5, message="zip1 must have 5 digits")
           public String getZip1() {
                return zip1;
           }
           public void setZip1(String zip1) {
                this.zip1 = zip1;
           }
           
           @Column(name="zip2")
           @Length(min=4, max=4, message="zip2 must have 4 digits")
           public String getZip2() {
                return zip2;
           }
           public void setZip2(String zip2) {
                this.zip2 = zip2;
           }
      



      This ends up only showing the appropriate error for the zip1 failure, and shows an empty message for the zip2 failure.


      If the validation is on the entity bean, it seems that I would have a hard time looking at the other fields in the entity bean in the setter methods to check if other fields besides the one I'm currently setting are valid.



      I am just starting to start to try code this up elegantly, but I hoped that maybe somebody would be able to give me some suggestions on how to handle forms that don't necessarily match up to the database/entities on a 1 to 1 formfield-databaseproperty basis.