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: