Carl,
You should use a validator on the input component and
validate the value passed into the backing bean. The validator would throw an
exception against the component which, in turn would cause the page to be
re-displayed and all non-validating error messages shown.
So..
create a validator method on your backing bean and change
the code in your JSP to
<h:inputText value="#{userActions.user.firstName}"
id="firstName" required="true" validator="{#userActions.validateFirstName}"
/>
<h:message for=""
styleClass="errors" />
and in your backing bean
create the validation logic eg.
public void validateFirstName(FacesContext fc, UIComponent uic, Object o) {
// Logic to determine if the input value validates.
if (o.toString().length()
< 3 || o.toString().length() > 8)
{
invalidateInput((UIInput) uic, fieldName + " must be between 3 and 8 characters
long.");
}
}
If your validation logic is
simple such as the one above you can use an existing validator in the JSF
package.
From: Carl Sziebert [mailto:[EMAIL PROTECTED]
Sent: Monday, May 08, 2006 6:02 PM
To: MyFaces Discussion
Subject: h:message and rendered flag
Is there a convenient way to check an individual
component for validity or errors in the rendered flag of the h:message
tag? My backing bean has getters/setters like
so:
private User user = new
User();
public User getUser() {
return
user;
}
public void setUser(User user)
{
this.user =
user;
}
And my JSP accesses the field like
so:
<h:inputText value="#{userActions.user.firstName}"
id="firstName" required="true" />
I've attempted the following with no
success:
<h:message for=""
styleClass="errors" rendered="#{!
userActions.user.firstName.valid}"
/>
I get the feeling that I should be doing things
differently or there is something trivial I am missing. Any and all help
is appreciated.
Thanks.
Carl