[英]JSF password confimation validation using ADF 11
如何创建验证器,验证用户是否在密码字段和密码确认字段中输入了相同的值?
我是在托管bean中完成的,但我更喜欢使用JSF验证器...
真正的问题是,如何创建一个验证器来访问除被验证组件之外的其他JSF组件?
我正在使用ADF Faces 11。
谢谢...
真正的问题是,如何创建一个验证器来访问除被验证组件之外的其他JSF组件?
不要试图直接访问组件; 你会后悔的。 JSF的验证机制最有效地防止垃圾进入模型。
您可以使用不同类型的托管bean; 形式的东西:
/*Request scoped managed bean*/
public class PasswordValidationBean {
private String input1;
private String input2;
private boolean input1Set;
public void validateField(FacesContext context, UIComponent component,
Object value) {
if (input1Set) {
input2 = (String) value;
if (input1 == null || input1.length() < 6 || (!input1.equals(input2))) {
((EditableValueHolder) component).setValid(false);
context.addMessage(component.getClientId(context), new FacesMessage(
"Password must be 6 chars+ & both fields identical"));
}
} else {
input1Set = true;
input1 = (String) value;
}
}
}
这是使用方法绑定机制绑定的:
<h:form>
Password: <h:inputSecret
validator="#{passwordValidationBean.validateField}"
required="true" />
Confirm: <h:inputSecret
validator="#{passwordValidationBean.validateField}"
required="true" />
<h:commandButton value="submit to validate" />
<!-- other bindings omitted -->
<h:messages />
</h:form>
将来,你应该可以使用Bean Validation( JSR 303 )来做这种事情。
您始终可以从上下文映射中提取其他字段的值,并在多个字段上执行验证。 如下所示:
public void validatePassword2(FacesContext facesContext, UIComponent uIComponent, Object object) throws ValidatorException{
String fieldVal = (String)object;
String password1 = (String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("password1");
if(!password1.equalsIgnoreCase(fieldVal)){
FacesMessage message = new FacesMessage("Passwords must match");
throw new ValidatorException(message);
}
}
并且jsf看起来像这样:
<h:inputSecret value="#{profile.password2}" validator="#{brokerVal.validatePassword2}" required="true" requiredMessage="*required" id="password2" />
HTH
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.