我有一个自定义验证器设置为在复合组件的元素上运行。它看起来像这样:
<myproject:myComponent>
<f:validator validatorId="customValidator" for="validatedField" />
</myproject:myComponent>
复合组件看起来像这样:
<composite:interface>
<composite:editableValueHolder name="validatedField" targets="validatedField" />
</composite:interface>
<composite:implementation>
<h:inputText id="validatedField" />
</composite:implementation>
它主要是有效的,但有一个打嗝:我如何根据需要指定一个字段?
看来,当我将验证字段完全留空时,根本不会调用自定义验证器。我尝试在<f:validateRequired for="validatedField" />
中添加mycomponent
,但它不起作用。
<小时/> 编辑(以回应BalusC):我的复合组件出现在其中的上下文的更详细描述:
<p:outputPanel id="pnlElements">
<ui:repeat value="#{backingViewBean.elementDtos}" var="elementDto">
<p:commandButton value="Edit..."
action="#{backingControllerBean.refreshView}"
ajax="true"
update=":frmMain:editElement"
oncomplete="wdgEditElement.show()">
<f:setPropertyActionListener
target="#higherLevelControllerBean.currentElementId}"
value="#{elementDto.Id}" />
</p:commandButton>
</ui:repeat>
</p:outputPanel>
<p:dialog widgetVar="wdgEditElement" modal="true">
<p:outputPanel id="editElement">
<myproject:myComponent>
<f:validator validatorId="customValidator" for="validatedField" />
</myproject:myComponent>
</p:outputPanel>
</p:dialog>
该页面显示了我创建的元素列表,每个元素都有一个编辑按钮(以及我省略的其他内容)。单击编辑按钮,弹出一个包含复合组件的窗口(以及一个保存按钮,为清楚起见再次省略),我可以在其中编辑元素。
问题是,当我将javax.faces.VALIDATE_EMPTY_FIELDS
设置为true
时,验证器会在填充编辑窗口的数据对象被设置之前被调用,并且它因为有空字段而咆哮。 (并在数据对象设置之前中止执行,因此所有字段都显示为空。)
(另外,我注意到当VALIDATE_EMPTY_FIELDS
设置为false
时,第二次及以后的时间我点击编辑按钮也会触发对验证器的调用。这看起来无害,但是不是我期待的行为。)
我在Glassfish下运行,所以我希望<f:validateRequired>
能够运行 - 所以也许我错了。我这样使用它:
<myproject:myComponent>
<f:validator validatorId="customValidator" for="validatedField" />
<f:validateRequired for="validatedField" />
</myproject:myComponent>
我做错了吗?
答案 0 :(得分:3)
只有在您的Web应用程序中启用了JSR303 bean验证时,<f:validateRequired>
才有效(在Java EE Web配置文件兼容服务器中通常就是这种情况,例如Glassfish,JBoss AS和TomEE,但因此不在简单的servletcontainers中)像Tomcat)。如果您的webapp不具备,那么您需要在web.xml
中手动强制对空字段进行bean验证:
<context-param>
<param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
<param-value>true</param-value>
</context-param>