Why does <h:inputText required=“true”> allow blank spaces?

When I set required="true" in a <h:inputText>, it still allows blank spaces. I have been trying to modify the jsf-api.jar but I could not understand how to generate new a JAR, so I tried to modify the isEmpty() method from UIInput class and compile it, open the jsf-api.jar and replace it with the new one, but it did not work.

What I need is to do trim() when the user writes in a <h:inputText> to do not allow blank spaces. How can I achieve this?

If you want to download the jsf-api.jar resource, you can do it, just read how to at: http://javaserverfaces.java.net/checkout.html.

Answers


That's normal and natural behaviour and not JSF specific. A blank space may be perfectly valid input. The required="true" only kicks in on empty inputs, not in filled inputs. In JSF you can however just create a Converter for String class to automatically trim the whitespace.

@FacesConverter(forClass=String.class)
public class StringTrimmer implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        return value != null ? value.trim() : null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return (String) value;
    }

}

Put this class somewhere in your project. It'll be registered automatically thanks to @FacesConverter and invoked automatically for every String entry thanks to forClass=String.class.

No need to hack the JSF API/impl. This makes no sense.


Need Your Help

JQuery UI Autocomplete multiple values

jquery autocomplete

I'm having a problem with the AutoComplete with multiple values.

webpage print without the header and footer from the browser

web printing

I have a webpage, and I am using JavaScript .print();. however, I don't want have the header and footer from the browser(date and url). I had some research from the internet, it is within the brows...

Try and Catch In Pascal

error-handling try-catch pascal

I'm using Dev-Pas 1.9.2 and am trying to make sure the program doesn't crash when a symbol or a letter value is entered.