9 Replies Latest reply on Jul 29, 2009 12:08 AM by asookazian

    Form validation does not work with Seam and Hibernate

    derkd

      Hi all,


      I have a web application which collects data from a webservice. I have a field which needs a minimum of 3 characters before it can make a call to the webservice.
      So I thought why not make a validation for this in the form and make use of the Hibernate Annotations. Is this even possible when you do not use a database at all and only use it for form validation?
      I use Seam 1.2.1GA with RichFaces and Hibernate Validator 3.0.0.GA.


      I have this action:




      @Name("zoekOpleidingAction")
      @Scope(ScopeType.CONVERSATION)
      public class ZoekOpleidingAction implements Serializable {
           
           private String zoekLettercombinatie;
           
           @DataModel(scope = ScopeType.PAGE)
           protected List<IOpleiding> lettercombinatieOpleidingen;
           
           @NotNull
           @Length(min=3, message="min 3")
           public String getZoekLettercombinatie() {
                return zoekLettercombinatie;
           }
      
           public void setZoekLettercombinatie(String zoekLettercombinatie) {
                this.zoekLettercombinatie = zoekLettercombinatie;
           }     
      
           @Size(min=1, max=10, message="#{['maxOpleidingenList.length']}")
           public List<IOpleiding> getLettercombinatieOpleidingen() {
                return lettercombinatieOpleidingen;
           }



      my page looks like this:




      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
          xmlns:s="http://jboss.com/products/seam/taglib"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:rich="http://richfaces.org/rich"
          xmlns:a4j="http://richfaces.org/a4j"
          template="../layout/layout.xhtml">
      <h:form>
              <h:panelGrid columns="1" width="100%">
                     <s:validateAll>
                          <s:decorate name="opleidingZoekOpleidingComb" layout="../layout/validate.xhtml">
                               <ui:define name="label">Zoekterm:</ui:define> 
                               <h:inputText id="zoekOpleidingComb" value="#{zoekOpleidingAction.zoekLettercombinatie}" onkeypress="return submitEnter(this, event, 'zoekLettercombButton')" 
                               style=" width : 350px;" required="true"/>
                          </s:decorate>                    
                          <a4j:commandButton id="zoekLettercombButton" value="Zoek" action="#{zoekOpleidingAction.findOpleidingLetterCombinatie}" 
                          type="submit" reRender="lettercombOpleiding, closedFacet, openFacet"
                          eventsQueue="opleidingLetterQueue" ignoreDupResponses="true"/>               
                          
                          
                           <rich:dataList id="lettercombOpleiding" var="_opleiding" value="#{lettercombinatieOpleidingen}">
                           
                                <a4j:commandLink id="opleidingComb" action="#{zoekOpleidingAction.retrieveOpleidingRelatieEnd(_opleiding, true)}" oncomplete="registreer('#{zoekOpleidingAction.opleidingRelaties.cdOpleidingsnaam}+#{zoekOpleidingAction.opleidingRelaties.omsOpleidingsnaam}+#{zoekOpleidingAction.opleidingDetail.opleidingNiveauBemiddeling.cdOpleidingNiveauBemiddeling}+#{_opleiding.cdOpleidingsnaam}+#{_opleiding.omsOpleidingsnaam}')">#{_opleiding.omsOpleidingsnaam}</a4j:commandLink>
                                
                                <h:outputText id="whiteSpace" value="   " />
                                <a4j:commandLink id="opleidingInfoLink" action="#{zoekOpleidingAction.retrieveOpleidingRelatie(_opleiding)}" 
                                        oncomplete="Richfaces.showModalPanel('informationOpleidingPanel');" reRender="selectedOpleiding"
                                        eventsQueue="opleidingLetterQueue" ignoreDupResponses="true">   
      
                                    <h:graphicImage id="informationComb" style="border-width:0" value="../images/I.png" />
                                      
                               
                               </a4j:commandLink> 
                          </rich:dataList>
                     </s:validateAll>
              </h:panelGrid>
      
          </h:form>



      the template I use for the page is:



      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <f:subview contentType="text/html"
              xmlns="http://www.w3.org/1999/xhtml"
              xmlns:ui="http://java.sun.com/jsf/facelets"
              xmlns:h="http://java.sun.com/jsf/html"
              xmlns:f="http://java.sun.com/jsf/core"
              xmlns:a4j="http://richfaces.org/a4j"
              xmlns:s="http://jboss.com/products/seam/taglib">
      
           <div class="body">
           <a4j:region>
               <script language="javascript">
                    A4J.AJAX.onError = function(req,status,message){  
                         alert("Er is een error opgetreden " + message);
                         return "../error.seam";  
                    };
               
                   A4J.AJAX.onExpired = function(loc, expiredMsg){
                       //alert('Uw sessie is verlopen (Layout)! U wordt teruggeleid naar deze pagina: '+loc);
      
                       //return loc;
                       //window.location = "../timeout.seam";
                       return "../timeout.seam";                 
                   };
               </script>
           </a4j:region>
                <ui:insert name="body"/>
           </div>
      
      
      </f:subview>





      the template I use to decorate the input field is this:


      <ui:composition  xmlns="http://www.w3.org/1999/xhtml"
                       xmlns:ui="http://java.sun.com/jsf/facelets"
                       xmlns:h="http://java.sun.com/jsf/html"
                       xmlns:f="http://java.sun.com/jsf/core"
                       xmlns:s="http://jboss.com/products/seam/taglib">
      
          <div class="prop">
      
              <s:label styleClass="name #{invalid?'errors':''}">
                  <ui:insert name="label"/>
                  <s:span styleClass="required" rendered="#{required}">*</s:span>
              </s:label>
      
              <span class="value #{invalid?'errors':''}">
                  <s:validateAll>
                      <ui:insert/>
                  </s:validateAll>
              </span>
      
              <span class="error">
                  <h:graphicImage value="../images/error.gif" rendered="#{invalid}" styleClass="errors"/>
                  <s:message styleClass="errors"/>
              </span>
      
          </div>
      
      </ui:composition>




      so when I fill in just 2 characters I should expect a message but I don't see a message at all. Can someone help me with this?





        • 1. Re: Form validation does not work with Seam and Hibernate
          accless

          i never put an hibernate annotation validation on my session bean, it was always on my entity-bean.


          could u try to set a jsf-validator in your inputtext-field?
          something like this





          <f:validateLength minimum="3" />




          The other thing is u are using an a:commandButton, so u must reRender your decorator after submit!
          Give ur decorator an id and put this id in the reRender-Attribute of your a:commandButton







          • 2. Re: Form validation does not work with Seam and Hibernate
            derkd

            I managed to get this working but not exactly the way I wanted.


            The annotations work.



            @NotNull
                 @Length(min=3, message="#{messages['zoekterm.length']}")
                 public String getZoekLettercombinatie() {
                      return zoekLettercombinatie;
                 }



            the page is looking like this now:




            <s:decorate id="idOpleidingZoekOpleidingComb" name="opleidingZoekOpleidingComb" layout="../layout/validate.xhtml">
                                     <ui:define name="label">Zoekterm:</ui:define> 
                                     <h:inputText id="zoekOpleidingComb" value="#{zoekOpleidingAction.zoekLettercombinatie}" onkeypress="return submitEnter(this, event, 'zoekLettercombButton')" 
                                     style=" width : 350px;" required="true"/>
                                </s:decorate>
                                <rich:messages/>                    
                                <a4j:commandButton id="zoekLettercombButton" value="Zoek" action="#{zoekOpleidingAction.findOpleidingLetterCombinatie}" 
                                type="submit" reRender="lettercombOpleiding, closedFacet, openFacet, idOpleidingZoekOpleidingComb"
                                eventsQueue="opleidingLetterQueue" ignoreDupResponses="true"/>



            As you can see I added the


            <rich:messages/>



            this will display the message from the annotation. And it is displaying it twice :( HOW CAN I GET RID OF IT DISPLAYING TWICE???


            although it is display a message now, it should work with the decorate instead of the


            <rich:messages>



            . I added the id of the decorate to the a4j:commandButton but that didn't helped. I think this is the nicest solution but I can't seem to get it working. Anybody ideas?


            • 3. Re: Form validation does not work with Seam and Hibernate
              cash1981

              The reason why it is displayed twice is because in your ../layout/validate.xhtml you have h:messages that will display the error message also.

              • 4. Re: Form validation does not work with Seam and Hibernate
                derkd

                Thanks for the response. I removed the s:messages but still get it twice. I think the ../layout/validate.xhtml is not even executed. Because if it was I should have a


                *



                after the input field because it is required. I don't see this now.

                • 5. Re: Form validation does not work with Seam and Hibernate
                  derkd

                  Is it possible that Seam 1.2.1 doesn't know about the



                  layout="../layout/validate.xhtml" 




                  attribute in the


                  <s:decorate />



                  tag?

                  • 6. Re: Form validation does not work with Seam and Hibernate
                    derkd

                    Stupid is I am, the layout attribute doesn't exist, not even in Seam2. That's because layout should be template :).


                    the next problem... It loads the page and I see the label


                    Zoekterm* 



                    2 times and once just the label


                    Zoekterm



                    Also I see 2 buttons...


                    So there is still something wrong.

                    • 7. Re: Form validation does not work with Seam and Hibernate
                      derkd

                      I found it !!!


                      It is a bug in facelets. Because I build with maven2 I downloaded the facelets from the maven repo. But in this version is a bug, so I replaced this one with the one from the seam dist. and now it works!!!



                      • 8. Re: Form validation does not work with Seam and Hibernate
                        derkd

                        Derk Dukker wrote on Jul 28, 2009 15:42:


                        I found it !!!

                        It is a bug in facelets. Because I build with maven2 I downloaded the facelets from the maven repo. But in this version is a bug, so I replaced this one with the one from the seam dist. and now it works!!!





                        https://jira.jboss.org/jira/browse/JBSEAM-1412

                        • 9. Re: Form validation does not work with Seam and Hibernate
                          asookazian

                          I was under the impression that Hibernate validator annotations do not belong on session beans (i.e. non-@Entity classes) but apparently I'm wrong:


                          https://jira.jboss.org/jira/browse/JBSEAM-458