我如何创建一个 validator 来验证用户是否在密码字段和密码确认字段中输入了相同的值?
我是在托管 bean 中完成的,但我更喜欢使用 JSF validator ...
真正的问题是,如何创建一个 validator 来访问除被验证组件之外的其他 JSF 组件?
我正在使用 ADF Faces 11。
谢谢...
最佳答案
The real question is, how to create a validator that access other JSF components other than the component being validated?
不要尝试直接访问组件;你会后悔的。 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;
}
}
}
这是使用方法绑定(bind)机制绑定(bind)的:
<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 ) 来完成这类事情。
关于java - 使用 ADF 11 的 JSF 密码确认验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1493047/