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

    Form validation does not work with Seam and Hibernate

    Derk Dukker Novice

      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?