2 Replies Latest reply on Aug 25, 2008 5:47 PM by nvcyril

    Validate dataTable-dataModel fields

    nvcyril

      I would like to know how it is possible to validate the fields of a dataTable with a validator function because i need to verify if some information exist in my database.


      list.xhtml :


      <h:dataTable rowKeyVar="row" id="myList" var="_item" value="#{myList}">
      <rich:column>
      <f:facet name="header">Value1</f:facet>
      <h:inputText id="value1" value="#{_item.value1}"/>
      </rich:column>
      <rich:column>
      <f:facet name="header">Value2</f:facet>
      <h:inputText  validator="#{myvalidator.validateValue2}" id="value2" value="#{_item.value2}"/>
      </rich:column>                                            
      </h:dataTable>



      MyValidator.java :



      @Name("myalidator")
      public class MyValidator 
      {
      public void validateValue2(FacesContext context, UIComponent component, Object value) throws javax.faces.validator.ValidatorException 
      {
      //i want to validate value2 with value1 here
      }
      }



      Maybe i can try to get the value1 into the value2 validator function thanks to the row index... but i need help for that too

        • 1. Re: Validate dataTable-dataModel fields
          michaelcourcy

          Did you try this ?


          <h:inputText  validator="#{myvalidator.validateValue2(_item)}" id="value2" value="#{_item.value2}"/>
          



          Passing the whole object to the method.



          • 2. Re: Validate dataTable-dataModel fields
            nvcyril

            yes but it doesn't work... i have found a other solution :


            In my validateValue2 function :



            UIData dataTable = (UIData)context.getViewRoot().findComponent("myForm:myList");
            myItemType currentItem = (myItemType)dataTable.getRowData();




            The validator runs for each item so, getRowData returns the current item.


            Thank you Michael !