【发布时间】:2011-01-28 05:09:55
【问题描述】:
由于我们在 Asp.Net 中有 comparevalidator,我们在 JSF 中有什么来验证两个字段的值是否相同?我想验证密码和确认密码字段。
【问题讨论】:
标签: jsf
由于我们在 Asp.Net 中有 comparevalidator,我们在 JSF 中有什么来验证两个字段的值是否相同?我想验证密码和确认密码字段。
【问题讨论】:
标签: jsf
不,这样的验证器在基本的 JSF 实现中不存在。您基本上需要在组的 last 组件上运行验证器,并使用 UIViewRoot#findComponent() 获取您想要验证的 other 组件。例如
public void validate(FacesContext context, UIComponent component, Object value) {
UIComponent otherComponent = context.getViewRoot().findComponent("otherClientId");
Object otherValue = ((UIInput) otherComponent).getValue();
// ...
}
有关更多背景信息和具体示例,另请参阅this article。
另一方面,如果您已经使用 JSF2,那么您也可以使用 ajaxical 验证:
<h:form>
<f:event type="postValidate" listener="#{bean.validate}" />
...
</h:form>
..where #{bean.validate} 方法如下所示:
public void validate(ComponentSystemEvent e) {
UIComponent form = e.getComponent();
UIComponent oneComponent = form.findComponent("oneClientId");
UIComponent otherComponent = form.findComponent("otherClientId");
// ...
}
有关更多 JSF2 验证示例,另请参阅 this article。
【讨论】: