1

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.

Zoltán
  • 21,321
  • 14
  • 93
  • 134

1 Answers1

3

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:form>
    <h:inputText id="foo" required="true" requiredMessage="Enter this!" />
    <h:message for="foo" />
    <h:commandButton value="Submit" />
</h:form>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 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 08:31