gpt4 book ai didi

java - JSF 中的交叉字段验证 h :dataTable

转载 作者:搜寻专家 更新时间:2023-10-31 20:22:40 24 4
gpt4 key购买 nike

我有一个 XHTML 页面,其中有四个文本框

<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>

我需要对开始日期和结束日期进行验证。如果用户在 id="startDate"& id="endDate"中输入一些开始和结束日期,可以说开始日期:"01/01/2012"(1jan) 和结束日期:01/31/2012 并且如果用户输入一些日期在 id="startDate1"和 id="endDate1"上 id="startDate1"应始终大于 id="endDate"即日期范围不应重叠

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;
}
}

不确定上面的 boolean 函数是否是当前的,是否可以在场景中工作。任何帮助将不胜感激。

最佳答案

我在这上面花了 6 个小时。但我解决了。我的英语不好。

  1. 在dataTable上添加绑定(bind)属性,指向backingbean中的UIDData字段(绑定(bind)表)

     <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}"**>
  2. 表单广告结束前

    <h:inputHidden id="validator" **validator="#{narocilo.validateKolicinaCena}"** value="morabiti"/>
  3. backing bean 中的 validator 方法

     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);}

关于java - JSF 中的交叉字段验证 h :dataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9224334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com