Take the 2-minute tour ×

I have a basic CRUD view-scoped bean. Within the setter methods I am performing some data-specific validation, which build a detailed error message for each setter if an error occurs in any of them.

This works fine, but I would like to empty this error message on each request and I have no idea how I would do that. preRenderView won't cut it, because this error message needs to be rendered as well. Something like a postRenderView would be ideal.

share|improve this question
Maybe this answer could help you to find your answer. –  Luiggi Mendoza Jul 30 '12 at 14:59

1 Answer 1

up vote 2 down vote accepted

You shouldn't perform validation in setter methods and you shouldn't store validation messages in the backing bean. Your whole problem is just caused by bad design and not utilizing JSF provided validation facilities.

Just utilize JSF provided validation facilities instead of working completely around it and all your problems as described so far will disappear. You can use several of the JSF builtin validators such as required="true", validator="javax.faces.XxxValidator, <f:validateXxx> tags, etc on input components. You can create a custom validator by implementing Validator interface and giving it an unique validator ID which you use in validator="myValidator" or <f:validator validatorId="myValidator">.

When using JSF standard validation, any validation error will be thrown as a ValidatorException with a FacesMessage in the request scope which would be shown in a <h:message> associated with the component. This way the messages will "automagically" disappear in the subsequent requests.

Here's a very basic kickoff example:

    <h:inputText id="foo" required="true" requiredMessage="Enter this!" />
    <h:message for="foo" />
    <h:commandButton value="Submit" />

See also:

share|improve this answer
Yes, the minute I wrote the question I remembered that JSF provides validators and realized I was going in the wrong direction. Thanks a lot for the answer! –  Zoltán Jul 31 '12 at 8:31

Your Answer


