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.
      <h:inputText id="userName" required="true"
      <s:validate />
      <h:message for="userName" styleClass="error" />

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

            <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;" />

            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.