如何通过ajax验证两个密码字段?

时间:2011-09-20 18:22:30

标签: jsf jsf-2 passwords validation

我正在尝试使用JSF验证两个密码字段,但直到现在还没有好处,我在google上搜索它但是一切都是关于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);
}

任何想法的人?

3 个答案:

答案 0 :(得分:19)

首先,使用 real Validator来验证输入。不要在动作事件方法中这样做。

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

这是一个启动示例:

binding

(请注意,我向两个组件添加了<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" /> ,并且还注意到您不一定需要将确认密码组件值绑定到托管bean属性,无论如何它都没有价值)

使用此验证器

required="true"

答案 1 :(得分:0)

使用seam 2,您可以使用组件<s:validateEquality>,而无需编写代码。对于JSF2,那么你有Seam 3模块,特别是Faces module和跨字段表单验证。一个例子:

首先,您必须使用 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>

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

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

答案 2 :(得分:0)

您可以使用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>