1

I have implemented a JSF validator based on this example.

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

I am using JSF 2.0 and Richfaces 4.1.0 Final.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ashish Agarwal
  • 6,215
  • 12
  • 58
  • 91

1 Answers1

4

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:

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?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the help, let me check and will update you, just a small follow up question, Can I add 2 Validator, 1 on keyup event and other one on Form Submit, if yes, then how? – Ashish Agarwal Jan 18 '12 at 04:07