Is there an easy way to subclass FieldLabel and override the renderComponent() method to move writeLabelPrefix and writeLabelSuffix to be inside <label>?

Thanks,

Matt

On May 31, 2006, at 2:51 PM, Ryan Holmes wrote:

Your problem getting the span tag where you want it is due to how the FieldLabel component renders itself. Here's the relevant part of FieldLabel.renderComponent():

       delegate.writeLabelPrefix(field, writer, cycle);
       writer.begin("label");
       if (id != null)
           writer.attribute("for", id);
       delegate.writeLabelAttributes(writer, cycle, field);
       renderInformalParameters(writer, cycle);
       writer.print(displayName, getRaw());
       writer.end();
       delegate.writeLabelSuffix(field, writer, cycle);


You could argue that writeLabelPrefix() and writeLabelSuffix() should both be inside the <label> tag (makes sense to me), but the current code is symmetrical and not necessarily "wrong." Your best bet is probably to override the FieldLabel component with your own version that renders the delegate prefix and/or suffix inside the label tag. And I'm sure you won't hesitate to file a bug report if you feel strongly about it ;)

-Ryan


Matt Raible wrote:

Matt Raible wrote:

Andreas Andreou wrote:

try component.getBinding("class")
if that's not null, do a getObject() on it



Thanks. This works, but it also prints out duplicate "class" attributes.

class="text large error" class="text large"


It'd be nice to have something like:

writer.appendAttribute("class", "values to append");

Another question - it seems my full <label> and <input> is getting wrapped with

<font color="red">

Is there anyway to get rid of that? It's problematic b/c the closing </font> isn't getting rendered until right before </form>.


Fixed this by using:

   public void writeLabelPrefix(IFormComponent component,
IMarkupWriter writer, IRequestCycle cycle) { // does nothing put prevent <font color="red"> from getting written
   }


Thanks,

Matt


Matt Raible wrote:

Hmmm, I'm guessing the answers to my questions below are "no, this isn't possible." ;-)

Here's another question - is it possible in my custom Validator to get the existing CSS classes on a component? For example, I currently have:

public void writeLabelAttributes(IMarkupWriter writer, IRequestCycle cycle, IFormComponent component) {
       if (isInError(component)) {
           writer.attribute("class", "error");
       }
   }

public void writeAttributes(IMarkupWriter writer, IRequestCycle cycle, IFormComponent component, IValidator validator) {
       if (isInError()) {
           writer.attribute("class", "error");
       }
   }

But I'd prefer to have something like this:

public void writeLabelAttributes(IMarkupWriter writer, IRequestCycle cycle, IFormComponent component) {
       if (isInError(component)) {
writer.attribute("class", "error " + component.getAttribute("class"));
       }
   }

public void writeAttributes(IMarkupWriter writer, IRequestCycle cycle, IFormComponent component, IValidator validator) {
       if (isInError()) {
writer.attribute("class", "error " + component.getAttribute("class"));
       }
   }

In other words, is there a method on component that allows me to get the current CSS classes?

Matt

Matt Raible wrote:

I have two different "Label" components I'm trying to create.
Issue #1
---------------------------
The first is a simple component (just a Label.jwc and Label.html file in WEB-INF) that writes a <label>. I'd like to include the option to write a "required" indicator. However, I'm having a difficult time retrieving a parameter's value. Here's what I have so far:

Label.jwc
<component-specification allow-informal-parameters="yes">
   <parameter name="key" required="yes"/>
   <parameter name="class" required="no"/>
<component id="label" type="Any" inherit-informal- parameters="yes"/>
</component-specification>

Label.html
<label jwcid="label"><span jwcid="@Insert" value="ognl:getMessages().getMessage(key)"/> <div jwcid="@If" condition="ognl:class == 'required'"><span class="req"> *</ span></label>

I'm pretty sure "class" won't work as a parameter name b/c I've seen a stacktrace using this already. Regardless, let's pretend it does (I've tried using other names). Here's the desired usage:

<label class="required desc" jwcid="@Label" for="phoneNumber" key="user.phoneNumber">Phone Number</label> produces:

<label class="desc" jwcid="@Label" for="phoneNumber">Phone Number<span class="req"> *</label>

I'm fine with the result having class="required desc" - I basically just need some indicator to show a field is required when I'm not wiring up it's input field as a component.

Issue #2
---------------------------
I'm overriding ValidationDelegate in order to add required field indicators. I used to be pre-pending an asterisk to the beginning of the field, and I had that working. Now I want to add <span class="req"> to the end of the <label> before the </ label> shows up. I'm using @FieldLabel and everything *almost* works. I've eliminating error styling in the class below so it's easier to read.

public class Validator extends ValidationDelegate {
   public void writeLabelSuffix(IFormComponent component,
IMarkupWriter writer, IRequestCycle cycle) {
       if (component.isRequired()) {
           writer.begin("span");
           writer.attribute("class", "req");
           writer.printRaw(" *");
           writer.end();
       }
   }

The code above results in <label>blah blah</label><span class="req"> *</span>, when I'd rather have the <span> w/in the <label>. I tried overriding writeLabelAttributes(), but that doesn't seem to allow me to create a new tag and write it out w/in the label. Am I missing something here? Has anyone done something like this - or knows how to add a <span> withing an @FieldLabel?

Thanks,

Matt






------------------------------------------------------------------ ---
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



------------------------------------------------------------------- --
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




-------------------------------------------------------------------- -
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to