我正在使用 primefaces 和 JSF。我有一个可选的地址字段。如果用户填写任何一个字段(例如 zip 或名称),那么所有其他字段都是必需的。进行此验证的最佳方法是什么。
问问题
2346 次
1 回答
1
您可以添加一个 postValidate 事件来验证多个字段,例如
<f:event listener="#{bean.validationMethod}" type="postValidate" />
<h:panelGroup id="xyz" rendered="#{facesContext.validationFailed}">
<h:message for="myform"/>
</h:panelGroup>
<h:panelGroup id="myform">
.......
<h:panelGroup>
这应该在模型更新之前触发,您可以获得不同组件的新值
FacesContext fc = FacesContext.getCurrentInstance();
UIComponent components = event.getComponent();
UIInput param1 = (UIInput) components.findComponent("param1");
UIInput param2 = (UIInput) components.findComponent("param2");
如果验证失败,调用 FacesContext.getCurrentInstance().validationFailed()
并为 myform 添加消息。
如果您没有使用自定义验证,只是想确保在填充特定输入时添加其他输入,那么我建议使用Omnifaces validateAllOrNone,
于 2013-03-29T17:33:01.180 回答