如何评估自定义组件中的FacesComponent属性

时间:2013-08-26 01:00:57

标签: jsf jsf-2 omnifaces

最近,我一直在研究动态表单项目,但是停止了自定义组件问题。我有什么:

formField的面部组件:

@FacesComponent(value = "formField")
public class FormFieldCompositeComponent { 
    private boolean email;
}

自定义组件jsf:

<o:validator for="email_text" validatorId="emailValidator" disabled="#{not cc.email}" />

OR

<c:if test="#{not cc.email}">
    <f:validator for="email_text" validatorId="emailValidator"></f:validator>
</c:if>

OR

<f:validator disabled="#{not cc.email}" for="email_text" validatorId="emailValidator"></f:validator>

验证员:

@FacesValidator("emailValidator")
public class EmailValidator implements Validator { }

我的问题是:

1。)如果我使用普通的f:验证器,就像我上面使用的那个,然后使用c:if启用/禁用它,那么它将无效。根据我读过的一些文章,因为f:验证器在构建时验证,而不是渲染时间。

2.。)如果我使用o:验证器,它可以工作,但问题是每次你点击提交一个新的无效电子邮件错误行被添加到p:消息。示例我单击提交按钮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:消息:

<p:messages id="formMessages" showDetail="true" showSummary="false" redisplay="false"></p:messages>

注意:
1.)我注意到验证器被称为n次,其中n是提交或点击完成的次数。

xhtml - https://github.com/czetsuya/crud-faces/blob/master/crud-faces/src/main/webapp/administration/user/user.xhtml

代码 - https://github.com/czetsuya/crud-faces/tree/master/crud-faces/src/main/webapp/resources/tags

bean组件 - https://github.com/czetsuya/crud-faces/tree/master/crud-faces/src/main/java/org/manaty/view/composite

1 个答案:

答案 0 :(得分:0)

好像没有f:验证器的机会,所以我推进了o:validator并提出了一个解决方法。如果错误已经存在于FacesMessages列表中,则需要捕获:

boolean match = false;
for (FacesMessage fm : context.getMessageList()) {
    if (fm.getDetail().equals(message)
            || fm.getSummary().equals(message)) {
        match = true;
        break;
    }
}

if (!match) {
    throw new ValidatorException(facesMessage);
}
相关问题