[Html-widget] Styling a RadioGroup in TemplateToolkit

Carl Franks fireartist at gmail.com
Tue Nov 7 09:22:27 GMT 2006


On 07/11/06, Xavier Robin <robin0 at etu.unige.ch> wrote:
> Hello,
>
> I'm using HTML::Widget in Catalyst, with TT2 as template to render the forms.
>
> I defined the following radio group :
>
> $w->element('RadioGroup', 'my_answer')->label('Answer')
>     ->values(qw/y n/)->labels(qw/Yes No/);
>
> and then :
>
> $c->stash->{widget_result} = $w->process($c->req)
>     if ($submitted && !$error);
> $c->stash->{widget_result} = $w->result else;
>
> And in the template :
>
> [% widget_result.elements("my_answer") %]
>
> I get that kind of thing :
>
> ·-----------------------·
> |  Answer               |
> |  Yes            *     |
> |  No             *     |
> ·-----------------------·
> (Error message)
>
> Now I would like to put "Answer" as a legend for the fieldset, in bold, the
> error message inside the fieldset and some comments after or before the radio
> buttons.
>
> ·--*Answer*-----------------·
> |  Some comment here        |
> |  Yes              *       |
> |  No               *       |
> |  (Error message)          |
> |  Some comment here        |
> ·---------------------------·
>
> I have found the following code working :
>
> <fieldset>
> <legend>
>     <strong class="required">
>         [% widget_result.elements('my_answer').label.content_list %]
>     </strong>
> </legend>
> <p>Some comment here</p>
> [% FOREACH radio = widget_result.elements('my_answer').element.content_list %]
>     [% FOREACH radio_element = radio.content_list %]
>         <label for="[% radio_element.for %]" id="[% radio_element.id %]">
>             [% radio_element.content.1 %]
>             [% FOREACH input = radio_element.content.0 %]
>                 <[% input.tag %] type="[% input.type %]"
>                     name="[% input.name %]" id="[% input.id %]"
>                     class="[% input.class %]" value="[% input.value %]">
>             [% END %]
>         </label>
>     [% END %]
> [% END %]
> <p>Some comment here</p>
> </fieldset>
>
>
> It is quite (very) tricky, but it works... as long as the form isn't
> submitted. When the form gets submitted and redisplayed, with errors, the
> fields are replaced by something like HTML::Element=HASH(0xb1a331c), because
> of a supplementary span element that breaks all my quirk trick.
>
> I think there is a simple way to access to :
>     1. The label of the radio group ("Answer")
>     2. Each field (label + radio button)
>     3. Errors (if any)
> but I can't find it in the docs or in the list archives.
>
> Could you give me a hint about that ?

I think creating your own Element package, as a sub-class of
RadioGroup might be the way to go. You could also look at the
t/lib/TestContainer.pm file for an example of using a custom container
to change the output.
As you've already found, changing elements once they're already
generated is very fragile, as you're basically parsing html - which is
difficult, and it means you're more susceptible to any changes we
might make.

You might want to keep an eye on the parse-html work I'm doing
(mentioned in the HTML::Widget::Element::Field thread), although it
still might require you creating your own Element package.

Also, I can't see anything in the code of RadioGroup.pm which would
add another span tag once the form's submitted, unless there was an
error.

Personally, I've never used $result->elements, and have only ever used
[% result %] in my templates.

Carl



More information about the Html-widget mailing list