1

必要な2つのフィールドに対して1つだけを表示しようとしています。現時点では、両方のフィールドが空の場合、2つのエラーメッセージが表示されます。両方または1つのフィールドだけが空の場合、メッセージが1つだけになるようにします。

コードは次のようになります。

<x:inputText
    value="#{bean.proxyUrl}"
    id="idProxyUrl"
    required="true"
    />
<x:outputText value=":" />
<x:inputText
    value="#{bean.proxyPort}"
    id="idProxyPort"
    required="true"
    />
<x:message for="idProxyUrl" errorClass="errorMessage" style="margin-left: 10px;" />
<x:message for="idProxyPort" errorClass="errorMessage" style="margin-left: 10px;" />

どちらか一方または両方のフィールドが空であるかどうかに関係なく、1つのメッセージしか受け取らないということについて私は何ができますか。

4

1 に答える 1

2

最初のコンポーネントのSubmittedValueをチェックする2番目のコンポーネントに特別なバリデーターを指定できます。対応する[パスワードの確認]フィールドをチェックするPasswordValidatorに対して同様のことを行いました。

@FacesValidator("passwordValidator")
public class PasswordValidator implements Validator {    

    @Override
    public void validate(FacesContext context, UIComponent component,
            Object value) throws ValidatorException {


        String password = (String) value;


        UIInput confirmComponent = (UIInput) component.getAttributes().get("confirm");
        String confirm = (String) confirmComponent.getSubmittedValue();

        if (password == null || password.isEmpty() || confirm == null || confirm.isEmpty()) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please confirm password", null);
            throw new ValidatorException(msg);
        }


        if (!password.equals(confirm)) {
            confirmComponent.setValid(false); 
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "The entered passwords do not match", null);
            throw new ValidatorException(msg);
        }


    }

他のコンポーネントの送信値を確認する必要がある理由は、ライフサイクルのプロセス検証フェーズでバリデーターが呼び出されるためです。このフェーズが完了し、送信されたすべての値が検証に合格するまで、送信された値は適用されません。

于 2012-09-28T14:35:28.027 に答える