per-tag "input required" messages?

Hi,

I have several <h:inputText required="true"> tags in the same <h:form>.

How can I set per-tag "Value is required" validation message?

ie:

username and e-mail h:inputText fields, I would like <h:messages> to output:

"User Name is required" //if username's missing

"Email is required" //if e-mail's missing

thanks

-nikita

[398 byte] By [nikita_bludevila] at [2007-10-3 0:36:49]
# 1

Nikita,

Here's one way to do it.

In the following example, it is a bit of work to get the validateRequired() method set up; but once you have it set up, you can re-use that validate method for any of your required application inputs.

JSP code:

<h:panelGroup>

<h:outputText value="Email Address" />

<h:inputText id="email" value="#{profile.email}" validator="#{someBean.validateRequired}" />

<h:message for="email" />

</h:panelGroup>

A description of the validator method:

public void validateRequired(FacesContext context, UIComponent component, Object value) {

// if "value" is null, or an empty string:

// get the parent of "component," that will be the HtmlPanelGroup created by the h:panelGroup tag

// look for an HtmlOutputText child component of the HtmlPanelGroup; that is the input label created by the h:outputText tag

// use the input label to create an error message for a ValidatorException and throw it

}

}

Hope this helps,

Scott

Scott_Moorea at 2007-7-14 17:30:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Scott,

reasonable thought but there is a problem:

validator method will only get called if user supplies a value with <input>. If user provides no input, then existing bean value is reused and validators aren't called.

So to fix that, we add "required="true" to h:inputText. Now, however, if user provides no input, our custom validator doesn't get called because the built-in validator fails. In this case the form is redisplayed with standard "Validation Error: Value is required." That message text can be changed, but it's still going to be same text for all components ;-

nikita_bludevila at 2007-7-14 17:30:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

You just need to add the "requiredMessage" attribute to the h:inputText tag, as follows:

<h:panelGroup>

<h:outputLabel for="email" value="Email Address" />

<h:inputText id="email" value="#{profile.email}" required="true" requiredMessage="Email is required" />

<h:message for="email" />

</h:panelGroup>

<h:panelGroup>

<h:outputLabel for="phone" value="Phone Number" />

<h:inputText id="phone" value="#{profile.phone}" required="true" requiredMessage="Phone number is required" />

<h:message for="phone" />

</h:panelGroup>

tony_robertsona at 2007-7-14 17:30:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Nice! Tony, how did you know about "requiredMessage" attribute? It doesn't apper to be listed in JSF taglib docs for h:inputText... Oh, I see - but it is in JSF SDK docs.
nikita_bludevila at 2007-7-14 17:30:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Nikita,

Shoot, you're right, sorry for the bad recommendation.

I should have known better--I now remember that I ran into that same issue (the fact that JSF will not trigger validation on empty fields) when working with validators awhile back.

Tony, good to know about the "requiredMessage" attribute.

Nikita, regarding your comment about it not being in the TLDs: I'm guessing it was a feature that was added in 1.2, since I see it in the 1.2 TLD docs for h:inputText:

http://java.sun.com/javaee/javaserverfaces/1.2/docs/tlddocs/h/inputText.html

and I don't see it in the h:inputText docs for 1.1_01:

http://java.sun.com/javaee/javaserverfaces/1.1_01/docs/tlddocs/h/inputText.html

Regards,

Scott

Scott_Moorea at 2007-7-14 17:30:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Right, this feature was added in 1.2.
rlubkea at 2007-7-14 17:30:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...