3 Replies Latest reply on Nov 6, 2008 9:54 PM by Dean Hiller

    s:validate and Hibernate Validators

    System Administrator Expert

      Seam 2.0.1.GA
      RF 3.2.0.SR1


      According to the ref doc for Seam 2.0.1.GA:


      <s:validate> Description
      A non-visual control, validates a JSF input field against the bound property using
      Hibernate Validator.
      Attributes
      None.
      Usage
      <h:inputText id="userName" required="true"
      value="#{customer.userName}">
      <s:validate />
      </h:inputText>
      <h:message for="userName" styleClass="error" />
      <s:



      I propose adding an onfail attribute to the s:validate tag so that javascript may be executed when a hibernate validator fails for a particular field.


      For example, say a Hibernate Validator for pattern regex is fired onblur event in an input field.  The value is incorrect so the message is applied to the FacesMessages object and displayed in the rich:messages component in the xhtml.


      I want to be able to know when this failure happens when using the s:validate tag and take appropriate action to the UI.


      How can I do this without the proposed enhancement?

        • 1. Re: s:validate and Hibernate Validators
          Dan Allen Master

          You can actually do this now. Seam just provides the information in a different way than you propose. If you wrap the field in an s:decorate tag, than within the Facelet component template the context variable invalid is set to either true or false depending on whether the validation failed. You can wrap JavaScript in a rendered block or you can use Ajax4jsf to fire the JavaScript function (I'm blanking at the moment on the exact technique).


          If one of the components was modified, it would need to be s:decorate not s:validate. The s:validate tag is just a JSF validator tag, so it is not rendered and therefore cannot put anything on the screen.

          • 2. Re: s:validate and Hibernate Validators
            System Administrator Expert

            I have seen the s:decorate tag in the booking example.  Here is the edit.xhtml that was being used in the s:decorate tag in book.xhtml.


            <!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: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="entry">
                    <s:label styleClass="label #{invalid?'errors':''}">
                        <ui:insert name="label"/>
                        <s:span styleClass="required" rendered="#{required}">*</s:span>
                    </s:label>
                    <span class="input #{invalid?'errors':''}">
                        <s:validateAll>
                            <ui:insert/>
                        </s:validateAll>
                    </span>
                    <s:message styleClass="error errors"/>
                </div>
                
            </ui:composition>



            <s:decorate id="checkinDateDecorate" template="edit.xhtml">
                                    <ui:define name="label">Check In Date:</ui:define>
                                    <rich:calendar id="checkinDate" value="#{booking.checkinDate}" required="true" datePattern="MM/dd/yyyy" event="onblur" reRender="checkinDateDecorate" style="width: auto;" />
                            </s:decorate>



            I'm not sure exactly how to use the s:decorate with the s:validate and the hibernate validator in my scenario/problem.



            I don't understand how you would apply the correct javascript function to the applicable field.  I didn't find a similar example in your book's PDF or elsewhere.


            BTW, your book was backordered on Amazon (as well as JBoss in Action) until next month....

            • 3. Re: s:validate and Hibernate Validators
              Dean Hiller Expert

              maybe dig into <a:support> on rich faces?  That is what triggers the onblur stuff and.  I am not sure myself exactly what you are trying to do, but we use <a:support to rerender the component after changes to it so errors can be shown dynamically.