Craig McClanahan wrote:

On Fri, 3 Dec 2004 13:21:16 -0600, Heath Borders
<[EMAIL PROTECTED]> wrote:


Unfortunately, the spec isn't very good on this matter.

Basically, you'll have to override the spec and provide custom
functionality to be able to achieve this.

Isn't this something we could do in the MyFaces custom components?

The reason you can't do this within the spec is that the Validation
exception that is created for empty UIInputs that are required is
created inside UIInput.  Unfortunately, there is no way to change the
String that the template message uses in its "{0}" place without
overriding UIInput's validate() method.  This, of course, means that
you would have to create extensions to all UIInputs
(HtmlSelectOneRadio, HtmlSelectOneListbox, HtmlInputText, etc).

Perhaps someone with a little clout (i.e. Craig, Manfred), could
suggest this to the spec guys, since I've tried to do so on the
forums, but it has fallen on deaf ears.




There actually is a way to change the message text (declare a <message-bundle> element in faces-config.xml, and in the corresponding ResourceBundle define messages for the keys that are described in Section 2.5.2.4 of the spec.

What you cannot currently change is what parameters are passed in to
replace the {0} text -- right now it's the component id.  Looking into
this was on the initial radar for the JSF 1.2 expert group, but don't
know if they've discussed it yet.

The particular idea of looking up the corresponding label component is
an interesting one, but presumes that the page author is diligent
about using <h:outputLabel> elements for their field labels, instead
of <h:outputText>.  So there would have to be a fallback mechanism as
well.

On the other hand, you can also "cheat" a little, and convince JSF to
use *your* component class instead of the standard one. Let's say
that you did a MyHtmlOutputText component that extended HtmlOutputText
and had the modified validate() method. To register it, just do this
in your faces-config.xml file:


Looking at MyFaces source code, this looks easy enough to change. In the UIInput class, just change the following to look up the label based on the id, then I'd just have to be smart about naming my input fields to match their i18n keys:

In the validate method:

if (isRequired() && empty)
{
_MessageUtils.addErrorMessage(context, this, REQUIRED_MESSAGE_ID,new Object[]{getId()});
setValid(false);
return;
}


Change this to:

if (isRequired() && empty)
{
_MessageUtils.addErrorMessage(context, this, REQUIRED_MESSAGE_ID, getMessageBasedOnId(getId())});
setValid(false);
return;
}


My question is - how would I get the message bundle?

A better way would be to look up the label for the input and get the rendered message from that. But I don't know if that's possible.

Matt

   <component>
       <component-type>javax.faces.HtmlOutputText</component-type>
       <component-class>com.mycompany.MyHtmlOutputText</component-class>
   </component>

and the JSF runtime, including <h:outputText>, will use your component
class instead of the standard one.  As long as you're subclassing the
one you replace, you should not have any class cast exception
problems.

Craig


Craig




On Fri, 03 Dec 2004 11:56:02 -0700, Matt Raible <[EMAIL PROTECTED]> wrote:


I would like to change the validation messages to be a bit more
friendly, like they are with Commons Validation and Struts/Spring.
Basically, instead of:

"firstName": Value is required.

I'd like it to say "First Name is a required field."

I have the following in my resource bundle, but it doesn't give me
access to the field's label (i18n-ized).

javax.faces.component.UIInput.REQUIRED={0} is a required field.

Instead it prints:

firstName is a required field.

Any idea how to look up a input field's label and use the text from that
in the {0}?

Thanks,

Matt




--
If you don't have a GMail account, I probably have 5 invites.  Just ask!
-Heath Borders-Wing
[EMAIL PROTECTED]







Reply via email to