Jsf PrimeFaces数据表-是否可以进行交叉验证?
我有一个带有输入字段的数据表。在提交表格时,我必须一起验证所有输入。验证没有问题,但是将ui状态错误放在无效字段上会带来麻烦。我尝试使用UIViewRoot#findComponent(id)确定组件,但此方法始终返回NULL。我从firebug那里得到了“id”,所以我不明白出了什么问题 我希望任何人都能帮助我 我的数据表:Jsf PrimeFaces数据表-是否可以进行交叉验证?,jsf,primefaces,Jsf,Primefaces,我有一个带有输入字段的数据表。在提交表格时,我必须一起验证所有输入。验证没有问题,但是将ui状态错误放在无效字段上会带来麻烦。我尝试使用UIViewRoot#findComponent(id)确定组件,但此方法始终返回NULL。我从firebug那里得到了“id”,所以我不明白出了什么问题 我希望任何人都能帮助我 我的数据表: <prime:dataTable id="dtSpielberichtErfassen2"
<prime:dataTable id="dtSpielberichtErfassen2"
value="#{bsvttController.spielDetailListe}" var="dtSpiErf2"
selection="#{bsvttController.selectedSpielDetail}" selectionMode="single"
rowKey="#{dtSpiErf2.id}">
<prime:column style="width:55px">
<h:outputText value="#{dtSpiErf2.spielPaarung}" />
</prime:column>
<prime:column style="width:180px"
headerText="#{bsvttController.mannschaftHeim}">
<h:panelGrid id="pgSpiErf1"
columns="1" cellpadding="0" cellspacing="0"
styleClass="#{dtSpiErf2.saetzeGewonnen == 3 ? 'fett' : null}">
<h:outputText value="#{dtSpiErf2.posHeim1}" />
<h:outputText value="#{dtSpiErf2.posHeim2}" />
</h:panelGrid>
</prime:column>
<prime:column style="width:180px"
headerText="#{bsvttController.mannschaftGast}">
<h:panelGrid id="pgSpiErf2"
columns="1" cellpadding="0" cellspacing="0"
styleClass="#{dtSpiErf2.saetzeVerloren == 3 ? 'fett' : null}">
<h:outputText value="#{dtSpiErf2.posGast1}" />
<h:outputText value="#{dtSpiErf2.posGast2}" />
</h:panelGrid>
</prime:column>
<prime:column style="width:50px"
headerText="1. Satz">
<h:panelGrid columns="3" cellpadding="0" cellspacing="0">
<pe:inputNumber id="inG1" value="#{dtSpiErf2.satz1Gewonnen}"
maxlength="2" decimalPlaces="0"
style="width:20px; text-align:right">
</pe:inputNumber>
<h:outputText value=":" />
<pe:inputNumber id="inV1" value="#{dtSpiErf2.satz1Verloren}"
maxlength="2" decimalPlaces="0"
style="width:20px; text-align:left">
</pe:inputNumber>
</h:panelGrid>
</prime:column>
<prime:column style="width:50px"
headerText="2. Satz">
<h:panelGrid columns="3" cellpadding="0" cellspacing="0">
<pe:inputNumber id="inG2" value="#{dtSpiErf2.satz2Gewonnen}"
maxlength="2" decimalPlaces="0"
style="width:20px; text-align:right">
</pe:inputNumber>
<h:outputText value=":" />
<pe:inputNumber id="inV2" value="#{dtSpiErf2.satz2Verloren}"
maxlength="2" decimalPlaces="0"
style="width:20px; text-align:left">
</pe:inputNumber>
</h:panelGrid>
</prime:column>
<prime:column style="width:50px"
headerText="3. Satz">
<h:panelGrid columns="3" cellpadding="0" cellspacing="0">
<pe:inputNumber id="inG3" value="#{dtSpiErf2.satz3Gewonnen}"
maxlength="2" decimalPlaces="0"
style="width:20px; text-align:right">
</pe:inputNumber>
<h:outputText value=":" />
<pe:inputNumber id="inV3" value="#{dtSpiErf2.satz3Verloren}"
maxlength="2" decimalPlaces="0"
style="width:20px; text-align:left">
</pe:inputNumber>
</h:panelGrid>
</prime:column>
<prime:column style="width:50px"
headerText="4. Satz">
<h:panelGrid columns="3" cellpadding="0" cellspacing="0">
<pe:inputNumber id="inG4" value="#{dtSpiErf2.satz4Gewonnen}"
maxlength="2" decimalPlaces="0"
disabled="#{bsvttController.disable2(dtSpiErf2, 4)}"
style="width:20px; text-align:right">
</pe:inputNumber>
<h:outputText value=":" />
<pe:inputNumber id="inV4" value="#{dtSpiErf2.satz4Verloren}"
maxlength="2" decimalPlaces="0"
disabled="#{bsvttController.disable2(dtSpiErf2, 4)}"
style="width:20px; text-align:left">
</pe:inputNumber>
</h:panelGrid>
</prime:column>
<prime:column style="width:50px"
headerText="5. Satz">
<h:panelGrid columns="3" cellpadding="0" cellspacing="0">
<pe:inputNumber id="inG5" value="#{dtSpiErf2.satz5Gewonnen}"
maxlength="2" decimalPlaces="0"
disabled="#{bsvttController.disable2(dtSpiErf2, 5)}"
style="width:20px; text-align:right">
</pe:inputNumber>
<h:outputText value=":" />
<pe:inputNumber id="inV5" value="#{dtSpiErf2.satz5Verloren}"
maxlength="2" decimalPlaces="0"
disabled="#{bsvttController.disable2(dtSpiErf2, 5)}"
style="width:20px; text-align:left">
</pe:inputNumber>
</h:panelGrid>
</prime:column>
</prime:dataTable>
我的验证方法的代码段:
// %1% - index of datatable row
// %2% - identifying part of componet id
String id = ":formBsvtt:caseUpdateSpielberichtErfassen:dtSpielberichtErfassen2:%1%:in%2%_input";
int saetzeGesamtGewonnen = 0;
int saetzeGesamtVerloren = 0;
int spieleGesamtGewonnen = 0;
int spieleGesamtVerloren = 0;
FacesContext fc;
FacesMessage fm;
UIComponent uic;
UIInput uii;
//
// Prüfung 1: Sätze korrekt? -> 11:n oder n:11 oder n+2:n(ab n=10)
for (int i = 0; i < spielDetailListe.size(); i++)
{
SpielDetail sd = spielDetailListe.get(i);
if (!istSatzende(sd.getSatz1Gewonnen(), sd.getSatz1Verloren()))
{
fc = FacesContext.getCurrentInstance();
fm = new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Je Satz sind mindestens 11 Punkte und eine Diffenrenz von 2 Punkten notwendig.", "");
fc.addMessage("", fm);
id = id.replace("%1%", String.valueOf(i)).replace("%2%", "G1");
uic = fc.getViewRoot().findComponent(id);
if (uic != null)
{
uii = (UIInput) uic;
uii.setValid(false);
}
return false;
}
}
/%1%-数据表行的索引
//%2%-正在识别组件id的一部分
字符串id=“:formBsvtt:caseUpdateSpielberichtErfassen:dtSpielberichtErfassen2:%1%:在%2%的输入中”;
int-saetzegesamtgewannen=0;
int-saetzeGesamtVerloren=0;
int spieleGesamtGewonnen=0;
int spieleGesamtVerloren=0;
FacesContext fc;
面部信息调频;
uic组件;
输入uii;
//
//Prüfung 1:Sätze korrekt?->11:n顺序n:11顺序n+2:n(ab n=10)
对于(int i=0;i
如果您实现一个独立的验证器并在datatable中的输入组件上声明该验证器,那么您的工作将更干净、更顺畅。您试图做的是不可维护并且不必要地将视图绑定到模型。我建议你
验证器
。下面只是一个虚拟的impl
@FacesValidator("myValidator")
public class MyValidator implements Validator{
public void validate(FacesContext context, UIComponent component,
Object value) throws ValidatorException {
for (int i = 0; i < spielDetailListe.size(); i++)
{
SpielDetail sd = spielDetailListe.get(i);
if (!istSatzende(sd.getSatz1Gewonnen(), sd.getSatz1Verloren()))
{
fc = FacesContext.getCurrentInstance();
fm = new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Je Satz sind mindestens 11 Punkte und eine Diffenrenz von 2 Punkten notwendig.", "");
fc.addMessage("", fm);
id = id.replace("%1%", String.valueOf(i)).replace("%2%", "G1");
uic = component.getClientId();
if (uic != null)
{
uii = (UIInput) uic;
uii.setValid(false);
}
}
@FacesValidator(“myValidator”)
公共类MyValidator实现验证器{
公共无效验证(FacesContext上下文、UIComponent组件、,
对象值)引发异常{
对于(int i=0;i
}
}<pe:inputNumber id="inV1" validator="#{myValidator}" value="#{dtSpiErf2.satz1Verloren}"
maxlength="2" decimalPlaces="0"
style="width:20px; text-align:left">
,让JSF来完成它的工作。样式和定位由您决定
<prime:message for="inV1"/>