如何通过确认密码验证

时间:2017-07-07 14:40:52

标签: validation jsf primefaces

我在JSF上有两个密码字段 - 以及它们的错误消息,所以如果确认密码不等于定义密码 - 屏幕上会出现错误消息。但即使字段等于但定义密码未通过验证(因此它传递给确认密码验证器为null),它也会出现

所以 - 这是我的定义密码JSF片段

 <div class="item"
 style="height: initial">
 <p:outputLabel id="l_passDef" for="passDef"
 value="#{msgs['customerForm.defaultPassword']}"/>
<p:password id="passDef" widgetVar="passDefVar" value="#{customerBean.customer.password}"
feedback="true" label="Default Password"
redisplay="true" required="true"
requiredMessage="#{msgs['Error.passDef.mandatory']}"
validatorMessage="#{msgs['Error.passDef.wrongFormat']}"
                                                binding="#{passDef}">
<p:ajax event="keyup" oncomplete="hideCustomerErrMsg('passDef')"/>
<f:validateRegex pattern="(?=.{8,})((?=.*\d)(?=.*[a-z])(?=.*[A-Z])|(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_])|(?=.*[a-z])(?=.*[A-Z])(?=.*[\W_])).*"/>
</p:password>
<div class="pass-message">
<p:message id="m_passDef" for="passDef" display="text"/>
                                    </div>
                                </div>

所以 - 这是我的确认密码JSF片段

<div class="item">
<p:outputLabel id="l_passConf" for="passConf"
value="#{msgs['customerForm.confirmPassword']}"/>
<p:password id="passConf" widgetVar="passConfVar" value="#{customerBean.customer.password}"
label="Confirm Password" redisplay="true" required="true"
requiredMessage="#{msgs['Error.passConf.mandatory']}"
                                                >
<f:attribute value="#{passDef}" name="passDef"/>
<f:validator binding="#{confirmPasswordValidator}"/>
<p:ajax event="keyup" oncomplete="hideCustomerErrMsg('passConf')"/>
</p:password>
<div class="pass-message">
<p:message id="m_passConf" for="passConf" display="text"/>
                                    </div>
                                </div>

这是我的验证器

@Component
@Scope("request")
public class ConfirmPasswordValidator implements Validator {

    private static final Logger LOGGER = Logger.getLogger(ConfirmPasswordValidator.class);

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        ResourceBundle resourceBundle = PropertyResourceBundle.getBundle("messages/messages_en");
        if (value == null) {
            return;
        }
        UIInput uiInput = (UIInput) component.getAttributes().get("passDef");
        String defaultPassword = (String) uiInput.getValue();
        String confirmPassword = (String) value;

        if ((!confirmPassword.equals(defaultPassword))&&(defaultPassword != null)) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "",
                    resourceBundle.getString("Error.passConf.confirm")));
        }
    }
}

如果密码字段相同但首先(定义)密码字段未通过验证,请帮助我隐藏错误信息

0 个答案:

没有答案