Dan*_*iel 10 validation jsf jsf-2
我采取了以下BalusC启动示例并通过添加提交按钮和其他h:消息并f:ajax从中h:inputSecret's删除了(f:ajax由于某种原因在我离开第一个h:inputSecret时立即显示"值是必需的"错误的原因删除了原因)第二h:inputSecret- 但用户没有机会输入... ??? < - 另一个未来的问题?:))
好吧,长话短说:
我试图找出如何在全局h:消息中显示关于密码字段(密码不相等)的验证错误,而不是在密码字段的个别h:消息中显示我确实需要的="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" />
Run Code Online (Sandbox Code Playgroud)
和附加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"/>
Run Code Online (Sandbox Code Playgroud)
@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."));
}
}
}
Run Code Online (Sandbox Code Playgroud)
也
谢谢你,
解决方案(感谢BalusC)
变
<f:attribute name="confirm" value="#{confirmPassword.submittedValue}" />
Run Code Online (Sandbox Code Playgroud)
至
<f:attribute name="confirm" value="#{confirmPassword}" />
Run Code Online (Sandbox Code Playgroud)
和
String confirm = (String) component.getAttributes().get("confirm");
Run Code Online (Sandbox Code Playgroud)
成
UIInput confirmPasswordComponent = (UIInput) component.getAttributes().get("confirm");
String confirm = (String) confirmPasswordComponent.getSubmittedValue();
Run Code Online (Sandbox Code Playgroud)
和
throw new ValidatorException(new FacesMessage("Passwords are not equal."));
Run Code Online (Sandbox Code Playgroud)
成
context.addMessage(null, new FacesMessage("Passwords are not equal."));
context.validationFailed();
((UIInput) component).setValid(false);
confirmPasswordComponent.setValid(false);
return;
Run Code Online (Sandbox Code Playgroud)
如果Validator特定组件上的某个组件抛出a ValidatorException,则FacesMessage它将自动与Validator调用该组件的组件相关联.
您需要手动添加FacesMessage一个null客户端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.
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,OmniFaces项目有一个<o:validateEqual>组件,应该使这不那么繁琐.另请参见展示示例.