Kush Sahu
Kush Sahu

Reputation: 399

How to validate blank field on bean side in jsf?

I have some input fields and some selectonemenu, I need to validate them and based on their validation I have to show a error message for whole page. I have to also show message with that inputfields and selectonemenu.

My validation is only for null and blank values. Since I want to show a message based upon null and blank fields I tried bean side validation. But my validation method is never called for blank fields.

Upvotes: 0

Views: 6892

Answers (1)

BalusC
BalusC

Reputation: 1108577

Just use required="true" on the input components.

<h:inputText id="input" ... required="true" />
<h:message for="input" />
<h:selectOneMenu id="menu" ... required="true" />
<h:message for="menu" />

You are not very clear about "bean side", the terminology used in the question is very poor and overly generic, but if you actually meant JSR303 bean validation and expecting the @NotNull to be triggered, then you need to add the following context parameter to web.xml:

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>

This way JSF will convert empty string submitted values to null before passing to model. The @NotNull will namely not kick in on empty strings.

See also:

Upvotes: 6

Related Questions