如何为复合组件内的输入组件指定验证器?

发布于 2024-12-26 03:31:06 字数 720 浏览 5 评论 0原文

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

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

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄凉少年不暖心 2025-01-02 03:31:06

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

您需要将所需的输入(您想要为其附加验证器)定义为 <复合:接口>

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

上面的 基本上告诉我们,任何 都必须应用于 。因此,应该执行以下操作:

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

您甚至可以在 nametargets 中使用相同的值,但关键点是必须有一个 <; composite:editableValueHolder> 的存在是为了让 JSF 知道验证器应该针对哪个输入组件,即复合体中可以有多个输入组件,您瞧。

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>

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>

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.

╰ゝ天使的微笑 2025-01-02 03:31:06

如果您正在使用 JSF 2,我认为您不需要修改faces-config.xml 文件来创建客户验证器。您可以简单地使用注释@FacesValidator来声明Validator。它应该是这样的:

@FacesValidator("myValidator")
public class MyValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        // Your logic
    }

}

然后您可以开始在 .xhtml 页面中使用它,例如 标记:

<f:validator validatorId="myValidator" />

If you are working with JSF 2, I don't think you need to touch the faces-config.xml file to create a customer Validator. You can simply use the annotation @FacesValidator to declare a Validator. It should be something like this:

@FacesValidator("myValidator")
public class MyValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        // Your logic
    }

}

Then you can start using it in your .xhtml page with, for instance, <f:validator> tag:

<f:validator validatorId="myValidator" />
糖粟与秋泊 2025-01-02 03:31:06

不,这对于 Jsf 2.0 来说是不必要的。只需使用 @FacesValidator 注释您的验证器。注释会自动注册您的验证器。不需要 XML。

No. That is not necessary with Jsf 2.0. Just annotate your validator with @FacesValidator. The annotation registers your validator automatically. No xml needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文