我应该在 faces-config.xml
中注册自定义验证器吗?如果我使用的是 JSF 2.0.4?
我的自定义验证器使用验证器接口(interface) javax.faces.validator.Validator
.
<cc:myComp id="customcomp1" ... />
<cc:myComp id="customcomp2" ...>
<f:validator id="myvalidator" for="myComp" />
</cc:myComp>
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>
最佳答案
根据更新问题中的代码示例,您似乎根本没有将验证器委托(delegate)给正确的输入,因此验证器完全被忽略了。
您需要将所需的输入(您想为其附加验证器)定义为 <composite:editableValueHolder>
在 <composite:interface>
.
<cc:interface>
<cc:editableValueHolder name="forName" targets="inputId" />
...
</cc:interface>
<cc:implementation>
...
<h:inputText id="inputId" ... />
...
</cc:implementation>
以上
<composite:editableValueHolder>
基本上告诉任何<f:validator for="forName">
必须应用于 <h:inputText id="inputId">
.所以,下面应该这样做:<cc:myComp>
<f:validator id="myValidator" for="forName" />
</cc:myComp>
您甚至可以在
name
中使用相同的值和 targets
,但关键是必须有<composite:editableValueHolder>
呈现,以便 JSF 知道验证器应该针对哪个输入组件,也就是说,复合 Material 中可以有多个输入组件,你看。
关于jsf - 如何为复合组件内的输入组件指定验证器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8780703/