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

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

问题描述

如果我使用的是JSF 2.0.4,是否应该在faces-config.xml中注册自定义验证器? 我的自定义验证程序使用的验证程序接口为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>

以上<composite:editableValueHolder>基本上表明必须在<h:inputText id="inputId">上应用任何<f:validator for="forName">.因此,然后应执行以下操作:

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中使用相同的值,但是关键是必须存在一个<composite:editableValueHolder>,以便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天全站免登陆