Le Dec 1, 2004, � 7:33 PM, Matt Raible a �crit :

Hello all,

I've been doing a bit of MyFaces development this week and enjoying it for the most part.

Glad to hear it Matt! 8-)

 I have a couple questions about <h:outputLabel>:

1. Is there anyway to modify h:outputLabel to add an asterisk for required fields?

You can do that without modifying h:outputLabel, for example:

   <h:loadBundle basename="messages" var="msgs"/>
   ...
   <h:outputLabel for="name" value="#{msgs.requiredNamePrompt}"/>

In your resource bundle:

   requiredNamePrompt=*Name

Of course with that solution you're assuming the required state of the field in the view. That's probably better left to business logic, so you could do this instead:

   <h:outputLabel for="name" value="#{backingBean.namePrompt}"/>

And BackingBean.getNamePrompt() would return the proper localized string, with an asterik if the property is required, and without an asterik otherwise.

Or, you could do this:

<h:panelGroup>
        <h:outputText value="*" rendered="#{backingBean.nameRequired}"/>
        <h:outputLabel value="#{msgs.namePrompt}"/>
</h:panelGroup>

And have backingBean implement a boolean method named isNameRequired that returns the required status of the property in question. The h:outputText will only render if its rendered attribute is true.

Do I have to subclass the existing JSP Tag to do this?

You hardly ever want to subclass an existing component tag, because tags are really just thin veneers for component/renderer pairs. You could, however, implement your own Label renderer and plug it into h:outputLabel. But I would opt for one of the easier solutions above.


2. Is it possible to auto-add a colon? I'm able to do this pretty easily with a custom taglib and Commons Validator, but it seems difficult with MyFaces? With Tapestry, I can subclass the existing component and add my own suffix (including a space before the colon for the French locale).

The same techniques for prepending an asterik will work for appending a colon. Again, you could implement your own Label renderer that does anything you want.



david

Thanks,

Matt




Reply via email to