如何在keyup事件上调用JSF验证程序? [英] How to invoke JSF validator on keyup event?

查看:87
本文介绍了如何在keyup事件上调用JSF验证程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经基于此示例实现了JSF验证器.

I have implemented a JSF validator based on this example.

如何在<h:inputText>的keyup事件中触发它?

How can I trigger it on keyup event of <h:inputText>?

我正在使用JSF 2.0和Richfaces 4.1.0 Final.

I am using JSF 2.0 and Richfaces 4.1.0 Final.

推荐答案

您可以通过

You can attach ajax listeners to DOM events on any JSF HTML input component by <f:ajax> tag.

<h:inputText id="foo" value="#{bean.foo}">
    <f:ajax event="keyup" execute="@this" render="fooMessage" />
    <f:validator validatorId="fooValidator" />
</h:inputText>
<h:message id="fooMessage" for="foo" />

fooValidator可以只是一个简单的您通过

The fooValidator can be just a simple Validator implementation which you register in the faces context by @FacesValidator annotation.

@FacesValidator("fooValidator")
public class FooValidator implements Validator {

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

        if (invalid) {
            throw new ValidatorException(new FacesMessage("Fail!"));
        }
    }

}

另请参见:

  • JSF 2中的通信-Ajax验证
  • See also:

    • Communication in JSF 2 - Ajax validation
    • 在RichFaces中,没有太大区别.在类固醇上只有<a4j:ajax>标记,基本上只是<f:ajax>.但这在这种特殊情况下并没有真正提供额外的好处.另请参见 f:ajax和a4j:ajax之间是否有区别?

      In RichFaces it's not much different. There's only the <a4j:ajax> tag which is basically just <f:ajax> on steroids. But it does not really provide additional benefits in this particular case. See also Is there any difference between f:ajax and a4j:ajax?

      这篇关于如何在keyup事件上调用JSF验证程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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