BalusC の回答の 1 つを参照します。JSF はクロスフィールド検証をサポートしていません。回避策はありますか?
私は同じ方法に従い、次のようなコードを作成します。
.xhtmlで
<h:form id="form1">
<div>
<p:messages globalOnly="true" display="text" />
<h:inputHidden value="true">
<f:validator validatorId="fooValidator" />
<f:attribute name="input1" value="#{input1}" />
<f:attribute name="input2" value="#{input2}" />
<f:attribute name="input3" value="#{input3}" />
</h:inputHidden>
<h:panelGrid columns="3">
<h:outputText value="name 1: " />
<p:inputText binding="#{input1}" id="input11" value="#{testPage.input1}" />
<p:message for="input11" display="text"/>
</h:panelGrid>
<h:panelGrid columns="3">
<h:outputText value="name 2: " />
<p:inputText binding="#{input2}" id="input22" value="#{testPage.input2}" />
<p:message for="input22" display="text"/>
</h:panelGrid>
<h:panelGrid columns="3">
<h:outputText value="name 3: " />
<p:inputText binding="#{input3}" id="input33" value="#{testPage.input3}" />
<p:message for="input33" display="text"/>
</h:panelGrid>
<p:commandButton value="Submit" action="#{testPage.submitValidator}" update=":updateBody" />
</div>
</h:form>
Java クラス:
@FacesValidator(value="fooValidator")
public class CustomValidator2 implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
UIInput input1 = (UIInput) component.getAttributes().get("input1");
UIInput input2 = (UIInput) component.getAttributes().get("input2");
UIInput input3 = (UIInput) component.getAttributes().get("input3");
Object value1 = input1.getSubmittedValue();
Object value2 = input2.getSubmittedValue();
Object value3 = input3.getSubmittedValue();
if (value1.toString().isEmpty() && value2.toString().isEmpty() && value3.toString().isEmpty()) {
String errorMsg = "fill in at least 1";
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMsg, errorMsg);
FacesContext.getCurrentInstance().addMessage("form1:input11", msg);
//throw new ValidatorException(msg);
}
}
}
コードは正常に機能していますが、問題に直面しています。検証が失敗した場合に JSF によって通常行われるように、name1 の入力テキスト (または name1 と name2 の両方の入力テキスト) の境界線を赤色で強調表示する方法。
参考画像:http: //img443.imageshack.us/img443/8106/errork.jpg
前もって感謝します