Please disable your adblock and script blockers to view this page

Search this blog

Tuesday 1 September 2015

Apply Validator to programmatically created ADF Faces components

Again a post in series of  Working with ADF Faces Components programmatically
Previously i have posted about Getting value , Applying Action Listener , Applying Client/Server Listener, Creating and applying client Attributes, Setting value expression  to programmatically created component
Now this post post is about applying Validator to a component that is created at run time
See how to do this (Jdev 12.1.3)-
  • First created a FusionWebApplication and a page in viewController project
  • Dropped a button on page , on this button action i will create an inputText programmatically and assign validator method reference to it
  • To create new inputText i have added following code (described in previous post)

  • /**Method to add dynamically created component to a parent layout
         * @param parentUIComponent
         * @param childUIComponent
        public void addComponent(UIComponent parentUIComponent, UIComponent childUIComponent) {

            //Code to create af:inputText and add it to panelgroup layout
            RichInputText ui = new RichInputText();
            //getParentGroupLayoutBind is the component binding of parent panel group layout
            addComponent(getParentGroupLayoutBind(), ui);

  • Next step is to create a validator method in managed bean to validate this inputText value, So i have created a validator method in managed bean that will check that inputText value should not be greater than 100

  •     /**Validator method to be applied on programmatically created inputText
         * @param facesContext
         * @param uIComponent
         * @param object
        public void testValidator(FacesContext facesContext, UIComponent uIComponent, Object object) {
            if (object != null) {
                Integer val = Integer.parseInt(object.toString());
                if (val > 100) {
                    throw new ValidatorException(new FacesMessage("Invalid Value","Must be less than 100"));

  • Now assign this validator method to programmatically created inputText, see how to do this
    Helper method to resolve Validator (import javax.faces.validator.Validator)

  • //Packages used
    import javax.el.ELContext;
    import javax.el.ExpressionFactory;
    import javax.faces.application.Application;
    import javax.el.MethodExpression;
    import javax.faces.context.FacesContext;
    import javax.faces.component.UIComponent;
    import javax.faces.validator.MethodExpressionValidator;
    /**Method to to resolve Validator method
         * @param actionName
        private Validator resolveValidator(String validatorName) {
            //Validator method takes 3 argument of following type , we have to define that
            Class[] argtypes = new Class[3];
            argtypes[0] = FacesContext.class;
            argtypes[1] = UIComponent.class;
            argtypes[2] = Object.class;
            FacesContext facesCtx = FacesContext.getCurrentInstance();
            Application app = facesCtx.getApplication();
            ExpressionFactory elFactory = app.getExpressionFactory();
            ELContext elContext = facesCtx.getELContext();
            MethodExpression methodExp = elFactory.createMethodExpression(elContext, validatorName, null, argtypes);
            return new MethodExpressionValidator(methodExp);

    To apply this validator to inputText just write this - Pass exact el reference of bean validator method to resolveValidator method

    //Pass string that contains proper el reference of Validator method 

  • All done :) Run and check application , On button click an inputText is created, i have entered a value greater than 100 and see the result

Cheers:) Happy Learning


  1. Hello thanks for sharing.
    I am currently trying to remove that popup (tooltip) that arises as the validation exception is thrown, how can I do that?
    Thanks in advance for any help!

    1. although I used the .resetValue() method the tooltip still shows up.

    2. So how do you want to show validation message ?

    3. Tahanks for your answer..
      I want to show the validation error message just as you do, throwing a validation exception, but after I throw it and I see the popup error message in the richinputtext I need to be able to remove that popup from showing up, so that the user cant see it no more, but I haven't been able to do so, what can I do to make the popup disappear ?

    4. But if don't want user to see the error popup then how'll he/she know that validation fails on this particular component ?

    5. I added a listener to another UIcomponent (lets call it C2) and if this other component (C2) changes its value I need the richinputtext to stop displaying the popup from the validation exception, I tried reseting the value of the richinputtext in the changelistener of the component C2 but it wouldn't work. It was fixed by adding the richinputtext as a partial target for the change listener of C2 after doing a resetValue() on the richinputtext.
      Thanks for your help and for this blog, I have found it very usefull.

  2. do you know any way to apply validator at runTime (Already defined in faces.config)to a component from bean method.