[html-formfu] proper way to set options for radiogroup

Carl Franks fireartist at gmail.com
Mon Sep 24 15:28:39 GMT 2007


On 24/09/2007, Brian Cassidy <brian.cassidy at nald.ca> wrote:
> Hey All,
>
> If someone could please refresh my memory, I'm attempting to populate a
> radiogroup with options from a catalyst model, setting defaults is an
> entry is in the stash.
>
> It shows the options okay, but it's not setting the default properly.
> I've examined $self->_options to see what comes out and the appropriate
> entry has default and value being equal (which i thought was the
> condition to make checked="checked" show up) ...
>
> Here is what I have -- i could've /sworn/ it was working earlier.
>
> ---
>
> package MyApp::Form::Element::AccountRoles;
>
> use strict;
> use warnings;
>
> use base qw( HTML::FormFu::Element::Radiogroup );
>
> sub process {
>      my $self = shift;
>
>      $self->container_tag( 'div' );
>      $self->attributes( { class => 'radiogroup' } );
>      $self->name( 'role' );
>
>      my $c = $self->form->stash->{ context };
>      my $roles
>          = $c->model( 'AccountRole' )->search( {}, { order_by => 'name' } );
>
>      my %uroles = eval {
>          map { $_->id => $_->id } $c->stash->{ account }->roles;
>      };
>
>      $self->options(
>          [   map {
>                  {   value            => $_->id,
>                      default          => $uroles{ $_->id } || undef,
>                      label            => ucfirst $_->name,
>                      attributes       => { id => 'role_' . $_->id, },
>                      label_attributes => { for => 'role_' . $_->id, },
>                  }
>                  } $roles->all
>          ]
>      );

No, 'default' would be ignored (lost) during options().
Only 1 radio item's value can match the default.
It should be:

    $self->options(
        [   map {
                {   value            => $_->id,
                    label            => ucfirst $_->name,
                    attributes       => { id => 'role_' . $_->id, },
                    label_attributes => { for => 'role_' . $_->id, },
                }
                } $roles->all
        ]
    );

    $self->default( $default );

(though I can't work out from your code what $default should be)

Carl



More information about the HTML-FormFu mailing list