Controlling the order of radio buttons in RadioButtonGroups

2 views
Skip to first unread message

Adam Prime

unread,
Feb 23, 2009, 12:10:54 AM2/23/09
to rose-htm...@googlegroups.com
It doesn't really look like there is any way (short of building my own
Field type) of getting a RadioButtonGroup to display it's items in an
order other than alphabetical. Am I missing something? I was trying to
do it just using the 'radio group' type.

$self->add_fields (
edition => {
type => 'radio group',
label => "Which edition...",
required => 1,
choices => {
'childrens' => "Children's Edition",
'young adult' => "Young adult edition",
'both' => "Both",
}
},
);

I'd also like it if html id's were implicitly created for the radio
items created like this, so that the label/input tags could be linked up
correctly. Is there a better approach I'm not seeing?

Adam


John Siracusa

unread,
Feb 23, 2009, 10:02:36 AM2/23/09
to rose-htm...@googlegroups.com
On Mon, Feb 23, 2009 at 12:10 AM, Adam Prime <adam....@utoronto.ca> wrote:
> It doesn't really look like there is any way (short of building my own
> Field type) of getting a RadioButtonGroup to display it's items in an
> order other than alphabetical. Am I missing something?

You are indeed. Just change those {} to []:

edition =>
{
type => 'radio group',
label => "Which edition...",
required => 1,
choices =>
[
'childrens' => "Children's Edition",
'young adult' => "Young adult edition",
'both' => "Both",

]
}

http://search.cpan.org/dist/Rose-HTML-Objects/lib/Rose/HTML/Form/Field/RadioButtonGroup.pm#add_radio_buttons

"A list or reference to an array containing a mix of value/label pairs"

> I'd also like it if html id's were implicitly created for the radio
> items created like this, so that the label/input tags could be linked up
> correctly. Is there a better approach I'm not seeing?

The first thing that springs to mind is to override the init() method
on your RadioButton class in your private library to set the id. (You
*do* have a private library, don't you? :)

http://search.cpan.org/dist/Rose-HTML-Objects/lib/Rose/HTML/Objects.pm#PRIVATE_LIBRARIES

Something like:

package My::HTML::Form::Field::RadioButton;
...
sub init
{
my($self) = shift;
$self->SUPER::init(@_);
$self->id(...auto-generate id somehow...) unless($self->id);
}

-John

Adam Prime

unread,
Feb 23, 2009, 10:35:23 AM2/23/09
to rose-htm...@googlegroups.com
John Siracusa wrote:
> On Mon, Feb 23, 2009 at 12:10 AM, Adam Prime <adam....@utoronto.ca> wrote:
>> It doesn't really look like there is any way (short of building my own
>> Field type) of getting a RadioButtonGroup to display it's items in an
>> order other than alphabetical. Am I missing something?
>
> You are indeed. Just change those {} to []:

I tried that last night and what I ended up with was 6 radio buttons,
not 3. I just upgraded to the most recent version of RHTMLO and i'm
still having the same result. with:

edition => {
id => 'edition',


type => 'radio group',

label => "Which edition ...",


required => 1,
choices => [
'childrens' => "Children's Edition",
'young adult' => "Young adult edition",
'both' => "Both",
]

},

I get the following xhtml. The order is preserved correctly, but the
pairs aren't getting paired up.:

<input name="Book.edition" type="radio" value="childrens" />
<label>childrens</label><br />
<input name="Book.edition" type="radio" value="Children's Edition" />
<label>Children's Edition</label><br />
<input name="Book.edition" type="radio" value="young adult" />
<label>young adult</label><br />
<input name="Book.edition" type="radio" value="Young adult edition" />
<label>Young adult edition</label><br />
<input name="Book.edition" type="radio" value="both" />
<label>both</label><br />
<input name="Book.edition" type="radio" value="Both" />
<label>Both</label></li>

If I change that to:

edition => {
id => 'edition',


type => 'radio group',

label => "Which edition ...",


required => 1,
choices => [

'childrens' => {
id => 'childrens',
label => "Children's Edition"
},
'young adult' => {
id => 'young adult',
label => "Young adult edition"
},
'both' => {
id => 'both',
label => "Both"
},
]
},

Then I get three radios with the order preserved, and I can stuff in
id's without subclassing RadioButtonGroup. yay. (though i could
probably actually write less code by subclassing radio button group to
insert the id's automatically...)

Adam

John Siracusa

unread,
Feb 23, 2009, 10:40:50 AM2/23/09
to rose-htm...@googlegroups.com
On Mon, Feb 23, 2009 at 10:35 AM, Adam Prime <adam....@utoronto.ca> wrote:
> I tried that last night and what I ended up with was 6 radio buttons,
> not 3.

Yes, maybe I should read my own documentation better, eh? :)

http://search.cpan.org/dist/Rose-HTML-Objects/lib/Rose/HTML/Form/Field/RadioButtonGroup.pm#add_radio_buttons

"Please note: the second form (passing a reference to an array)
requires that at least one item in the referenced array is not a plain
scalar, lest it be confused with "a reference to an array of
containing only plain scalar values."

(I even used bold text, heh.)

-John

John Siracusa

unread,
Feb 23, 2009, 10:44:12 AM2/23/09
to rose-htm...@googlegroups.com
On Mon, Feb 23, 2009 at 10:35 AM, Adam Prime <adam....@utoronto.ca> wrote:
> choices => [
> 'childrens' => {
> id => 'childrens',
> label => "Children's Edition"
> },
> 'young adult' => {
> id => 'young adult',
> label => "Young adult edition"
> },
> 'both' => {
> id => 'both',
> label => "Both"
> },
> ]
> },
>
> Then I get three radios with the order preserved, and I can stuff in
> id's without subclassing RadioButtonGroup. yay.

Earlier you asked about getting the ids "implicitly created" so I
didn't suggest this, but yeah, you can always explicitly set
attributes.

-John

Reply all
Reply to author
Forward
0 new messages