如何通过ajaxvalidation两个密码字段?

我试图用JSFvalidation两个密码字段,但直到现在没有好的,我在谷歌上search它,但一切都是关于JSF 1.2和相当混乱,我使用JSF 2.0。

这是我到目前为止所做的:

<h:outputLabel for="password" value="Password:" /> <h:inputSecret id="password" value="#{register.user.password}" > <f:ajax event="blur" listener="#{register.validatePassword}" render="m_password" /> </h:inputSecret> <rich:message id="m_password" for="password"/> <h:outputLabel for="password_2" value="Password (again):" /> <h:inputSecret id="password_2" value="#{register.user.password_2}" > <f:ajax event="blur" listener="#{register.validatePassword}" /> </h:inputSecret> 

这就是我的控制器:

 public void validatePassword() { FacesMessage message; if (!user.getPassword().equals(user.getPassword_2()) ){ message = new FacesMessage(FacesMessage.SEVERITY_ERROR, null, "different password"); }else{ message = new FacesMessage(FacesMessage.SEVERITY_INFO, null, "ok"); } FacesContext.getCurrentInstance().addMessage("form:password", message); } 

任何想法的家伙?

首先,使用一个真正的 Validator来validationinput。 不要在动作事件方法中做到这一点。

至于你的具体问题,你只需要在<f:ajax>execute属性中指定这两个字段,它只默认当前的组件。 如果您将validation程序附加到第一个input,并将第二个input的值作为<f:attribute> ,那么您将能够在validation程序中获取它。 您可以使用binding属性将组件绑定到视图。 这样你可以通过UIInput#getSubmittedValue()传递它的提交值。

这是一个开球的例子:

 <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}" /> <f:ajax event="blur" execute="password confirm" render="m_password" /> </h:inputSecret> <h:message id="m_password" for="password" /> <h:outputLabel for="confirm" value="Password (again):" /> <h:inputSecret id="confirm" binding="#{confirmPassword}" required="true"> <f:ajax event="blur" execute="password confirm" render="m_password m_confirm" /> </h:inputSecret> <h:message id="m_confirm" for="confirm" /> 

(请注意,我向这两个组件添加了required="true" ,并且还要注意,您不一定需要将confirm password组件值绑定到托pipebean属性上,否则无论如何都毫无价值)

与这个validation器

 @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.")); } } } 

缝2你有组件<s:validateEquality> ,你不需要编写代码。 对于JSF2,你有Seam 3模块,特别是Faces模块和Cross-field表单validation。 一个例子 :

首先你必须使用s:validateForm标签:

 <h:form id="passwordForm"> <h:inputSecret id="newPassword" required="true" redisplay="true" value="#{passwordController.newPassword}"> </h:inputSecret> <h:inputSecret id="confirmationPassword" value="#{passwordController.confirmPassword}" required="true" redisplay="true"> </h:inputSecret> <h:commandButton id="submit" value="Submit" action="#{passwordController.submitPassword}" /> <s:validateForm validatorId="passwordValidator" /> </h:form> 

以及上面的密码表单的相应validation器如下所示:

 @FacesValidator("PasswordValidator") public class PasswordValidator implements Validator { @Inject @InputField private String newPassword; @Inject @InputField private String confirmPassword; @Override public void validate(final FacesContext context, final UIComponent comp, final Object values) throws ValidatorException { if (!confirmPassword.equals(newPassword)) { throw new ValidatorException(new FacesMessage("Passwords do not match!")); } } } 

您可以使用Primefaces p:密码标签。 请参阅演示示例 。 它具有应该是确认密码的id的匹配属性。

 <p:panel header="Match Mode"> <p:messages id="messages" showDetail="true" autoUpdate="true"/> <h:panelGrid columns="2" id="matchGrid"> <h:outputLabel for="pass" value="Password " /> <p:password id="pass" value="#{passwordBean.password}" match="confirmPass" required="true"/> <h:outputLabel for="confirmPass" value="Confirm Password " /> <p:password id="confirmPass" value="#{passwordBean.confirmPassword}" required="true"/> </h:panelGrid> <p:commandButton id="saveButton" update="matchGrid" value="Save" /> </p:panel>