2011-09-20 30 views
15

我试图用JSF验证两个密码字段,但直到现在没有好的,我在谷歌搜索它,但一切都是关于JSF 1.2和相当混乱,我使用JSF 2.0。如何通过ajax验证两个密码字段?

这是我在做什么至今:

 <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); 
} 

任何想法的家伙?

+0

你是什么意思,它工作,但不是很好?它慢吗?不知道我明白你想改变什么,为什么。 – jdias

+0

嗨@jdias,我的意思是不按照它假设的方式工作。谢谢你可能有点困惑。 –

回答

19

首先,使用realValidator来验证输入。不要在动作事件方法中使用它。

关于你的具体问题,你只需要在<f:ajax>execute属性来指定领域,它即默认为仅当前组件。如果您将验证程序附加到第一个输入并将第二个输入的值作为<f:attribute>发送,那么您将能够在验证程序中获取它。您可以使用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"这两个部件,也请注意,你不一定需要确认密码组件值绑定到托管bean的属性,它的毫无价值那边反正)

与此验证

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

} 
+0

确认密码在binding =“#{confirmPassword}”中是什么? –

+1

@Jword:它将通过给定的标识符将组件绑定到视图,以便它可以通过视图中其他位置的“#{confirmPassword}”获得(例如在第一个字段的“”中,以便它可以在验证器)。 – BalusC

+0

我现在明白'捆绑'BalusC的功能,感谢帮助伙计。 –

0

缝2你有组件<s:validateEquality>,你不需要编写代码。对于JSF2,你有Seam 3模块,特别是Faces module和Cross-field Form 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> 

和上述密码表单对应的验证程序是这样的:

@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!")); 
     } 
    } 
} 
+0

我在纯JSF2中寻找类似的解决方案,但找不到它。你知道这是否存在于JSF2中?特别是@InputField注解似乎很有用。 –

0

您可以ü se Primefaces p:密码标签。请参阅演示example。它有匹配属性,它应该是确认密码的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>