如何为复合组件内的输入组件指定验证器? [英] How to specify a validator for an input component inside a composite component?

查看:26
本文介绍了如何为复合组件内的输入组件指定验证器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用的是 JSF 2.0.4,我应该在 faces-config.xml 中注册一个自定义验证器吗?我的自定义验证器使用 Validator 接口,即 javax.faces.validator.Validator.

Should I register a custom validator in faces-config.xml if I'm using JSF 2.0.4? My custom validator uses Validator interface which is javax.faces.validator.Validator.

<cc:myComp id="customcomp1" ... />

<cc:myComp id="customcomp2" ...>
    <f:validator id="myvalidator" for="myComp" />
</cc:myComp>

myComp.xhtml

myComp.xhtml

<cc:interface>
    <cc:attribute ... />
    <!-- more attributes -->
</cc:interface>
<cc:implementation>
    <h:panelGroup layout="block">
        <h:inputText id="firstName" ... />
        <h:inputText id="middleName" ... />
        <h:inputText id="lastName" ... />
    </h:panelGroup>
</cc:implementation>

推荐答案

根据更新问题中的代码示例,您似乎根本没有将验证器委派给正确的输入,因此验证器被完全忽略.

As per the code example in your updated question, you seem to not be delegating the validator to the right input at all, so the validator is simply ignored altogether.

您需要将所需的输入(您希望为其附加验证器)定义为 中.

You need to define the desired input (for which you'd like to attach a validator) as a <composite:editableValueHolder> in the <composite:interface>.

<cc:interface>
    <cc:editableValueHolder name="forName" targets="inputId" />
    ...
</cc:interface>
<cc:implementation>
    ...
    <h:inputText id="inputId" ... />
    ...
</cc:implementation>

上面的 基本上说明任何 都必须应用在<;h:inputText id="inputId">.因此,应该执行以下操作:

The above <composite:editableValueHolder> basically tells that any <f:validator for="forName"> must be applied on the <h:inputText id="inputId">. So, the following should then do it:

<cc:myComp>
    <f:validator id="myValidator" for="forName" />
</cc:myComp>

你甚至可以在nametargets中使用相同的值,但关键是必须有一个 出现以便 JSF 知道验证器应该针对哪个输入组件,也就是说,复合中可以有多个输入组件,你看.

You can even use the same value in name and targets, but the key point is that there must be a <composite:editableValueHolder> present so that JSF knows on what input component exactly the validator should be targeted, there can namely be more than one input component in the composite, you see.

这篇关于如何为复合组件内的输入组件指定验证器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆