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) {
            parentUIComponent.getChildren().add(childUIComponent);
            AdfFacesContext.getCurrentInstance().addPartialTarget(parentUIComponent);
        }
    




            //Code to create af:inputText and add it to panelgroup layout
            RichInputText ui = new RichInputText();
            ui.setId("pit1");
            ui.setContentStyle("width:200px;color:navy");
            //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 
    ui.addValidator(resolveValidator("#{viewScope.Testbean.testValidator}"));
    


  • 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