Validation 如何显示ValidatorException和required=";“真的”;在不同的消息元素中使用相同的输入字段

Validation 如何显示ValidatorException和required=";“真的”;在不同的消息元素中使用相同的输入字段,validation,jsf,jsf-2,Validation,Jsf,Jsf 2,我接受了以下内容并对其进行了一些修改,添加了一个提交按钮和额外的h:messages,并从h:inputSecret的中删除了f:ajax(删除了f:ajax,因为某种原因,当我离开第一个h:inputSecret时,它会立即显示“value is required”第二个h:inputSecret错误-但是用户没有机会在…?中键入它。如果某个特定组件上的验证器抛出一个验证器异常,那么它的面消息将自动与调用验证器的组件相关联 您需要在null客户端ID上手动添加FacesMessage,以便它最

我接受了以下内容并对其进行了一些修改,添加了一个提交按钮和额外的h:messages,并从
h:inputSecret的
中删除了
f:ajax
(删除了
f:ajax
,因为某种原因,当我离开第一个
h:inputSecret
时,它会立即显示“value is required”第二个
h:inputSecret
错误-但是用户没有机会在…?中键入它。如果某个特定组件上的
验证器
抛出一个
验证器异常
,那么它的
面消息
将自动与调用
验证器
的组件相关联

您需要在
null
客户端ID上手动添加
FacesMessage
,以便它最终位于
中。您还需要在
FacesContext
上手动设置
validationFailed()
,以便JSF不会更新模型值,也不会调用该操作。如果需要(尽管建议这样做),您还需要手动将组件标记为无效,以便任何适当的侦听器/树访问者(例如用于突出显示)都会考虑到这一点

if (!password.equals(confirm)) {
    context.addMessage(null, new FacesMessage("Passwords are not equal."));
    context.validationFailed();
    ((UIInput) component).setValid(false);
    confirmPasswordComponent.setValid(false); // You'd need to pass it as component instead of as its submitted value in f:attribute.
}

顺便说一句,该项目有一个
组件,这样应该可以减少繁琐的工作。另请参见。

您需要将其作为组件传递,而不是作为其提交的值在f:attribute中传递。如何传递?
<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"/>
<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;
if (!password.equals(confirm)) {
    context.addMessage(null, new FacesMessage("Passwords are not equal."));
    context.validationFailed();
    ((UIInput) component).setValid(false);
    confirmPasswordComponent.setValid(false); // You'd need to pass it as component instead of as its submitted value in f:attribute.
}