次のBalusC キックオフの例を取り上げて、送信ボタンと追加の h:messages を追加し、f:ajax
からを削除することで少し変更しました(最初のままにすると、何らかの理由h:inputSecret's
で原因が削除され、すぐに「値が必要です」というエラーが表示されます) 2番目-しかし、ユーザーはそれを入力する機会がありません... ??? <-別の将来の質問?:) )f:ajax
h:inputSecret
h:inputSecret
わかりました、長い話を短くするために:
パスワードフィールドの個々の h:message ではなく、グローバル h:messages の両方のパスワードフィールド (パスワードが等しくない) に関する検証エラーを表示する方法を見つけようとしています。各フィールドの に ="true" が表示され<h:message
ます...
しかし、現在、検証メッセージ(私の例外によってスローされた)とrequired="true"が同じ場所に表示されています
コードは次のとおりです。
<h:outputLabel for="password" value="Password:" />
<h:inputSecret id="password" value="#{bean.password}" required="true">
<f:validator validatorId="confirmPasswordValidator" />
<f:attribute name="confirm" value="#{confirmPassword.submittedValue}" />
</h:inputSecret>
<h:message id="m_password" for="password" />
<h:outputLabel for="confirm" value="Password (again):" />
<h:inputSecret id="confirm" binding="#{confirmPassword}" required="true">
</h:inputSecret>
<h:message id="m_confirm" for="confirm" />
そして、そのコードh:commandButton
の下に追加:h:messages
<h:commandButton value="doSomething" action="#{myBean.myAction}">
<f:ajax execute="password confirm" render="m_password m_confirm"></f:ajax>
</h:commandButton>
<h:messages globalOnly="true" styleClass="validation_value_required"/>
@FacesValidator("confirmPasswordValidator")
public class ConfirmPasswordValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
String password = (String) value;
String confirm = (String) component.getAttributes().get("confirm");
if (password == null || confirm == null) {
return; // Just ignore and let required="true" do its job.
}
if (!password.equals(confirm)) {
throw new ValidatorException(new FacesMessage("Passwords are not equal."));
}
}
}
また
よろしくお願いします。
解決策 (BalusC のおかげ)
かわった
<f:attribute name="confirm" value="#{confirmPassword.submittedValue}" />
に
<f:attribute name="confirm" value="#{confirmPassword}" />
と
String confirm = (String) component.getAttributes().get("confirm");
の中へ
UIInput confirmPasswordComponent = (UIInput) component.getAttributes().get("confirm");
String confirm = (String) confirmPasswordComponent.getSubmittedValue();
と
throw new ValidatorException(new FacesMessage("Passwords are not equal."));
の中へ
context.addMessage(null, new FacesMessage("Passwords are not equal."));
context.validationFailed();
((UIInput) component).setValid(false);
confirmPasswordComponent.setValid(false);
return;