Search code examples
jsf-2faceletsrenderer

Trim submitted value in Mojarra TextRenderer


We have many XHTML pages, each page has some <h:inputText>, <h:inputSecret>, etc.. input components.

I am trying to find out a generic way to trim all those values before form submission.

I implemented a TrimInputTextRenderer class,

XHTML page:

<h:inputText value="#{userBean.emailAddress}" trim="true">
       <f:validator validatorId="emailAddressValidator"/>
</h:inputText>

TrimInputTextRenderer.java:

package com.vyan.web.component;

import java.io.IOException;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.render.FacesRenderer;

import com.sun.faces.renderkit.html_basic.TextRenderer;

@FacesRenderer(componentFamily="javax.faces.Input", rendererType="javax.faces.Text")
public class TrimInputTextRenderer extends TextRenderer {

    private void encodeBeginForChildren(FacesContext context, UIComponent component) {
            if (component instanceof UIInput) {
                component = (UIInput)component;
                String componentValue = ((UIOutput)component).getValue().toString();
                componentValue = (componentValue!=null)?componentValue.trim():componentValue;
                ((UIInput)component).setValue(componentValue);
            }

            // Encoding recursively all childrens.
            if (component.isRendered() && component.getChildCount() > 0)
                encodeBeginForChildrens(context, component.getChildren());

    }

    private void encodeBeginForChildrens(FacesContext context, List<UIComponent> children) {
        for (UIComponent comp : children) {
            if (comp instanceof UIInput) {
                comp = (UIInput) comp;
                ((UIInput) comp).setValue(((UIInput) comp).getValue()
                        .toString().trim());
            }

                    // Encoding recursively all childrens.
            if (comp.isRendered() && comp.getChildCount() > 0)
                encodeBeginForChildrens(context, comp.getChildren());
        }

    }

    @Override
    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
        String trimValue = (String) component.getAttributes().get("trim");
        if (trimValue!= null && Boolean.valueOf(trimValue)) {
            //super.write(string);
            encodeBeginForChildren(context, component);
        } 
        super.encodeBegin(context, component);
    }

}

faces-config.xml:

<render-kit>
        <renderer>
            <component-family>javax.faces.Input</component-family>
            <renderer-type>javax.faces.Text</renderer-type>
            <renderer-class>com.vyan.web.component.TrimInputTextRenderer</renderer-class>
        </renderer>
    </render-kit>

The problem is, everything works fine, the custom renderer trims the values. But we need the component values in validator & managed bean. Those were getting executed before the method TrimInputTextRenderer#encodeBegin() method call. So in validator & managed bean, we are getting non-trimmed values. Is there a way it can be executed before validation phase, or do i need to implement this logic some where?


Solution

  • This logic needs to be implemented in the decode() method or, easier, the setSubmittedValue().

    @Override
    public void setSubmittedValue(UIComponent component, Object value) {
        if (value instanceof String) {
            value = ((String) value).trim();
        }
    
        super.setSubmittedValue(component, value);
    }
    

    The easier and non-implementation-dependent alternative to this Mojarra-specific renderer extension was been a @FacesConverter(forClass=String), by the way.