【发布时间】:2013-08-26 01:00:57
【问题描述】:
最近,我一直在处理一个动态表单项目,但因自定义组件问题而停止。我有什么:
formField的faces组件:
@FacesComponent(value = "formField")
public class FormFieldCompositeComponent {
private boolean email;
}
一个自定义组件jsf:
<o:validator for="email_text" validatorId="emailValidator" disabled="#{not cc.email}" />
或
<c:if test="#{not cc.email}">
<f:validator for="email_text" validatorId="emailValidator"></f:validator>
</c:if>
或
<f:validator disabled="#{not cc.email}" for="email_text" validatorId="emailValidator"></f:validator>
验证器:
@FacesValidator("emailValidator")
public class EmailValidator implements Validator { }
我的问题是:
1.) 如果我使用普通的 f:validator,就像我上面使用的那样,然后使用 c:if 来启用/禁用它,那么它将无法工作。根据我读过的一些文章,这是因为 f:validator 在构建时间验证,而不是在渲染时间验证。
2.) 如果我使用 o:validator,它可以工作,但问题是每次您点击提交时,都会将一个新的无效电子邮件错误添加到 p:messages。示例我点击提交按钮 3 次,然后收到 3 次电子邮件错误。
有什么想法吗?
更多信息(项目剖析) 示例我有一个带有字段电子邮件的页面用户,它将包括以下自定义组件:
+user.xhtml
+formPanel
+formField (this is where the validator is defined)
+formButtons (the action button)
+p:messages is defined
user.xhtml
<formPanel>
<formField field="email" />
<formButtons />
</formPanel>
命令按钮就像(formButtons):
<p:commandButton id="saveButton" rendered="#{cc.attrs.edit}"
value="#{messages['action.save']}"
action="#{cc.attrs.backingBean.saveOrUpdate()}" icon="ui-icon-check"
ajax="#{cc.attrs.ajaxSubmit}">
<c:if test="#{cc.attrs.backingBean.lcid != null}">
<f:param name="cid" value="#{cc.attrs.backingBean.lcid}" />
</c:if>
</p:commandButton>
formPanel 上定义的 p:messages:
<p:messages id="formMessages" showDetail="true" showSummary="false" redisplay="false"></p:messages>
注意:
1.) 我注意到验证器被调用了 n 次,其中 n 是提交或点击完成的次数。
标签 - https://github.com/czetsuya/crud-faces/tree/master/crud-faces/src/main/webapp/resources/tags
【问题讨论】:
-
也许在
p:messages上使用redisplay=false。 -
您好,p:messages 的重新显示已经设置为 false,所以这不是问题。
-
如您在上一个关于此主题的问题中提出的,请发布 SSCCE。在这种情况下,
for属性的使用非常奇怪,以至于我无法重新创建您的代码,因此无法重现您的问题。至少,for属性只能在模板客户端上的复合实现外部使用。另请参阅真实世界示例:stackoverflow.com/questions/8780703/… -
嗨,Balus,很抱歉。我正在更新上面的帖子以获取真实世界的示例。
-
哦,好的,您正在嵌套复合材料。这只是一些不是 SSCCE 风格的代码,但我现在可以从某个地方开始。如果我有更多的时间,我会仔细观察。