6

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

前もって感謝します

4

1 に答える 1

8

UIInput#setValid()を渡して、それらを無効としてマークしますfalse

input1.setValid(false);
input2.setValid(false);
input3.setValid(false);

境界線は PrimeFaces に固有<p:inputText>であるため、他の回答者が示唆するように CSS ボイラープレートを追加する必要はありません。

これは、カスタム バリデータを自作する必要なく、OmniFaces<o:validateAll>タグでも実現できることに注意してください。

于 2013-01-17T15:34:48.473 に答える