6 Replies Latest reply on Sep 15, 2009 2:01 PM by tobi04

    validateDoubleRange in rich:extendedDataTable?

      Hi

      in my richfaces site I have a little problem with an <f:validateDoubleRange> in an <rich:extendedDataTable>.

      The table:
      <rich:extendedDataTable
      ....
      <rich:column>
      <!-- shown value of max is 8.5 (bean type java.lang.Double) -->
      <h:outputText value="#{bean.max}" />

      <h:inputText id="price" value="#{bean.value}" size="5">
      <!-- works fine-->
      <f:validateDoubleRange minimum="0" maximum="10.5"/>
      <!-- shown error message maximum=0 -->
      <f:validateDoubleRange minimum="0" maximum="#{bean.max}"/>

      <rich:ajaxValidator event="onblur"/>
      </h:inputText>
      <rich:message for="price" />
      </rich:column>

      In the above example is the little problem, that the EL #{bean.max} does not work with f:validateDoubleRange, but the value is not 0 as h:outputText shows.

      What do I forget?


      Best regards
      Tobi

        • 1. Re: validateDoubleRange in rich:extendedDataTable?
          nbelaevski

          Hi Tobi,

          Does this work outside rich:extendedDataTable?

          • 2. Re: validateDoubleRange in rich:extendedDataTable?

            Hi nbelaevski,

            no, the following example does not work, too. In the second column #{bean.max} has the right value:

            <h:dataTable value="#{bean.testList}" var="bean"
             binding="#{bean.testTable}">
             <h:column>
             <h:inputText id="price" value="#{bean.value}" size="5">
            
             <f:validateDoubleRange minimum="0" maximum="#{bean.max}"/>
             <rich:ajaxValidator event="onblur"/>
            
             </h:inputText>
             <rich:message for="price" />
             </h:column>
             <h:column>
             <h:outputText value="#{bean.max}" />
             </h:column>
             </h:dataTable>


            Best regards
            Tobi

            • 3. Re: validateDoubleRange in rich:extendedDataTable?
              nbelaevski

              Hi Tobi,

              The second code snippet sheds some light on the problem - f:validateDoubleRange won't work correctly with the dynamic range, because range values are evaluated when the validator is created and "bean" iteration variable is not available at this point. Workarounds: call some method that will validate the value of "bean" object or try providing validator using EL-expression and create new instance of validator according to the allowed range each time.

              • 4. Re: validateDoubleRange in rich:extendedDataTable?

                Hi nbelaevski,

                thanks for your replys!

                Can you please give an short example of your first approach "call some method that will validate"?


                In your second approach (own validator) I see the the difficulty to get the value of #{bean.max) in the java code:

                ManagedBean orderBean:
                ------------

                public void myValidateDoubleRange(FacesContext cxt, UIComponent comp, Object obj) throws ValidatorException {

                ...

                xhtml:
                ------
                <h:inputText id="price" value="#{bean.value}" size="5" validator="orderBean.myValidateDoubleRange" />



                On the jsf site I am working with
                <rich:ajaxValidator event="onblur"/>
                , so the java method myValidateDoubleRange() get called every time I leave the input field.
                So, in myValidateDoubleRange() I have to know, which #{bean.max) value belongs to the current input field.

                How can I get access to #{bean.max) in the java method myValidateDoubleRange()? With FacesContext, ...?


                Best regards
                Tobi

                • 5. Re: validateDoubleRange in rich:extendedDataTable?
                  nbelaevski

                  Either check "eval" method from this article: http://www.ilikespam.com/blog/el-vs-dependency-injection or create method that will validate content in "bean" itself - http://www.ibm.com/developerworks/library/j-jsf3/ - "Validation methods in backing beans".

                  • 6. Re: validateDoubleRange in rich:extendedDataTable?

                    Hi nbelaevski,

                    thanks for the links!

                    With the eval("#{bean.max)") method I got what I want in myValidateDoubleRange() validation method in the backing bean.
                    http://www.ilikespam.com/blog/el-vs-dependency-injection

                    "eval" method:

                     /**
                     * Shorthand for Application.evaluateExpressionGet(...) which automatically casts the result to
                     * expected type and uses the current FacesContext for evaluating the expression.
                     *
                     * @param expression
                     * the EL expression to evaluate
                     * @param clazz
                     * the expected resultant class
                     */
                     @SuppressWarnings("unchecked")
                     public static <T extends Object> T eval(String expression, Class<T> clazz) {
                     return (T) FacesContext.getCurrentInstance().getApplication().evaluateExpressionGet(
                     FacesContext.getCurrentInstance(), expression, clazz);
                     }
                    


                    Best regards
                    Tobi