gpt4 book ai didi

validation - 如何在不同的消息元素中显示相同输入字段的 ValidatorException 和所需的 ="true"

转载 作者:行者123 更新时间:2023-12-03 15:46:51 27 4
gpt4 key购买 nike

我拿了以下BalusC kickoff example并通过添加提交按钮和其他 h:messages 并删除 f:ajax 对其进行了修改。来自 h:inputSecret's (由于某种原因删除了 f:ajax 原因,当我离开第一个 h:inputSecret 时,它立即显示第二个 h:inputSecret 的“需要值”错误 - 但用户没有机会输入... ??? <- 另一个 future 的问题?:) )

好的,长话短说:

我试图弄清楚如何在全局 h:messages 而不是在密码字段的单个 h:message 中显示关于两个密码字段(密码不相等)的验证错误
我确实希望 required="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:commandButtonh: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;

最佳答案

如果 Validator在特定组件上抛出 ValidatorException ,然后是 FacesMessage将自动与 Validator 所在的组件相关联。被调用。

您需要手动添加 FacesMessagenull客户端 ID,以便它以 <h:messages globalOnly="true"> 结尾.您还需要手动设置validationFailed()FacesContext以便 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.
}

顺便说一句, OmniFaces项目有一个 <o:validateEqual>应该使这不那么乏味的组件。另见 showcase example .

关于validation - 如何在不同的消息元素中显示相同输入字段的 ValidatorException 和所需的 ="true",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10007438/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com