views:

34

answers:

3

I have a simple JSF+RichFaces form with some fields and obviously a backing bean to store them. In that bean all the necessary properties have validation annotations (jsr303/hibernate), but I can't seem to find an annotation which would check if the property (String) is blank. I know there's a @NotBlank annotation in spring modules, but JSF doesn't support spring validation. Is there any easy way to check it or should I write my own annotation?

@Edit: I already tried @NotNull and @NotEmpty from jsr303 and hibernate, but they both failed I still can send a blank string like " ".

A: 

Perhaps @NotEmpty ?

Which is defined as:

@NotNull
@Size(min=1)

Since you are using richfaces, I guess you are using <rich:beanValidator />? It handles JSR 303 annotations.

Update: Try (taken from here):

@Pattern(regex="(?!^[\s]*$)"). 
Bozho
Tried it, it doesn't work unfortunately.
Zenzen
how did you try it, actually
Bozho
@NotEmpty allows strings with white-space through i.e. " " is considered valid.
GaryF
+1  A: 

I assume you're using JSF 1.2. Your best bet is then creating a converter which returns null for empty inputs.

public class EmptyToNullConverter implements Converter {

    public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
        if (value == null || value.trim().isEmpty()) {
            if (component instanceof EditableValueHolder) {
                ((EditableValueHolder) component).setSubmittedValue(null);
            }
            return null;
        }
        return value;
    }

    public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
        return (value == null) ? null : value.toString();
    }

}

Which needs to be registered in faces-config.xml as follows:

<converter>
    <converter-for-class>java.lang.String</converter-for-class>
    <converter-class>com.example.EmptyToNullConverter</converter-class>
</converter>

The above is not possible on JSF 1.1 or older because of the inability (by design) to register converters for java.lang.String.

If it might happen that you're already on JSF 2.0, then just adding the following context parameter to the web.xml should be sufficient to achieve (less or more) the same. Less or more, because it doesn't trim strings before testing on emptiness.

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>
BalusC
Yeah I'm using JSF2.0, but it doesn't work as a blank string " " isn't treated as an empty/null one. I will try to use @Length from Hibernate.
Zenzen
That requirement was not clear in your initial question. You can just use the converter :) But if applicable, the `@NotBlank` suggestion is *much* better.
BalusC
Well it's hard for me to argue here, as English isn't my native language, but I always thought that a string containing only spaces/enters/etc are also considered blank strings (and get reduced to 0 after trimming). Anyway the @NotBlank annotation from Hibernate 4.1 works fine.
Zenzen
+5  A: 

If you use Hibernate Validator 4.1 as your JSR-303 implementation, they provide a @NotBlank annotation that does EXACTLY what you're looking for, separate from @NotNull and @NotEmpty. You need to be using the (currently) latest version, but that will work.

If you can't go to the latest version for some reason, it doesn't take much to write an annotation yourself.

GaryF
a, so they added it ;) +1
Bozho
Thanks, strange I couldn't find it in google.
Zenzen