Cross field validation in JSF h: dataTable

I have an XHTML page that has four text fields

<h:column > <f:facet name="header"> <h:outputText value="Start Date" /> </f:facet> <h:inputText id="startDate" value="#{sampleVO.startDate}" /> </h:column> <h:column > <f:facet name="header"> <h:outputText value="End Date" /> </f:facet> <h:inputText id="endDate" value="#{sampleVO.endDate}" /> </h:column> <h:column > <f:facet name="header"> <h:outputText value="Start Date" /> </f:facet> <h:inputText id="startDate1" value="#{sampleVO.startDate}" /> </h:column> <h:column > <f:facet name="header"> <h:outputText value="End Date" /> </f:facet> <h:inputText id="endDate1" value="#{sampleVO.endDate}" /> </h:column> 

I need to put validation on the Start Date and End Date. If the user enters some start and end date in id = "startDate" and id = "endDate", we can say that the start date is "01/01/2012" (1jan) and the end date is: 01/31/2012, and if the user enters some dates on id = "startDate1" and id = "endDate1" id = "startDate1" should always be greater than thn id = "endDate", i.e. date ranges should not overlap

 public class SampleServiceimpl implements SampleService { private List<SampleVO> listOfSample; //this function gets called when clicked on Submit button on XHTML page public Void submit() { // how can i call datesOverLaps function to check entered // start date and End date not overlapping, Also how to // display the error message on XHTML page using FacingContext } public boolean datesOverlaps(List<SampleVO> sampleVO) { final Date early = sampleVO.getStartDate(); final Date late = sampleVO.getEndDate(); for(SampleVO existing : sampleVO) { if(!(early.isAfter(existing.getEnd())) || (late.isBefore(existing.getStart()))){ return true; } } return false; } } 

Not sure if the above logical function is current and will work in a script. Any help would be appreciated.

+4
source share
1 answer

I spend 6 hours on it. But I decided that. My English is bad.

  • Add a binding attribute on the dataTable, pointing to the UIDData field in the backingbean (binding table)

      <h:dataTable value="#{narocilo.podatki}" id="tabela" var="podatek" border="0" bgcolor="#F6FAF0" style="width:100%;" cellspacing="0" cellpadding="2" headerClass="header" rowClasses="#{narocilo.rowClasses}" columnClasses="align_left,align_right,align_center,align_right,align_right,align_center_konec" **binding="#{narocilo.podatkiData}"**> 
  • Until the end of the ad form

     <h:inputHidden id="validator" **validator="#{narocilo.validateKolicinaCena}"** value="morabiti"/> 
  • bean-supported validator method

      public void validateKolicinaCena(FacesContext aContext,UIComponent aComponent,Object aValue) { int shrani_index = fPodatkiData.getRowIndex(); UIComponent column_kol = fPodatkiData.getChildren().get(2); UIComponent column_cen = fPodatkiData.getChildren().get(5); for(int i=0; i<fPodatkiData.getRowCount(); i++) { fPodatkiData.setRowIndex(i); UIInput kolicina_input = (UIInput) column_kol.findComponent("kolicina"); UIInput cena_input = (UIInput) column_cen.findComponent("cena"); Object kolicina = kolicina_input.getLocalValue(); Object cena = cena_input.getLocalValue(); if(kolicina == null && cena != null) { kolicina_input.setValid(false); FacesMessage sporocilo = Sporocila.getMessage("si.alkimisticus.bitea.error", "javax.faces.component.UIInput.REQUIRED", null); sporocilo.setSeverity(FacesMessage.SEVERITY_ERROR); aContext.addMessage(kolicina_input.getClientId(aContext), sporocilo); } else if(kolicina != null && cena == null) { cena_input.setValid(false); FacesMessage sporocilo = Sporocila.getMessage("si.alkimisticus.bitea.error", "javax.faces.component.UIInput.REQUIRED", null); sporocilo.setSeverity(FacesMessage.SEVERITY_ERROR); aContext.addMessage(cena_input.getClientId(aContext), sporocilo); } else { kolicina_input.setValid(true); cena_input.setValid(true); } } fPodatkiData.setRowIndex(shrani_index);} 
+2
source

Source: https://habr.com/ru/post/1395729/


All Articles