how to invoke JSF Validator on keyup event?

夙愿已清 提交于 2019-12-24 16:41:18

问题


I am using JSF 2.0 and Richfaces 4.1.0 Final

Though some similar questions exist like Link
I want to implement JSF Validator on inputtext box on keyup event. Can any body give a pointer of some useful tutorial please?


回答1:


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

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!"));
        }
    }

}

See also:

  • Communication in JSF 2 - Ajax validation

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?



来源:https://stackoverflow.com/questions/8904658/how-to-invoke-jsf-validator-on-keyup-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!