Customised choices in form element?

12 views
Skip to first unread message

James Masters

unread,
May 3, 2009, 4:41:16 AM5/3/09
to Rose::HTML::Objects
I have a form that works nicely but I would like to add a Radio Button
field to it with the choices taking values that are dependent upon the
database object that is initialised with the form. The choices hash
can be generated easily given the database object. But I suppose that
since the form is generated with the following 2 lines, the only way
for this to work would be if I could pass the $dbobj to the "new"
method somehow.

my $form = $formclass->new;
$form->init_with_object($dbobj);

Is this possible? I suppose another way (although less preferable for
me) would be to generate the choices hash first and then pass this
somehow though using the "new" method. Is that possible?

thanks for any advice.

James.

John Siracusa

unread,
May 3, 2009, 8:20:55 AM5/3/09
to rose-htm...@googlegroups.com

There's no reason you can't do your form manipulations in
init_with_object(). Just have your radio button group field
("my_field") all ready and waiting in the form, then set the choices
in init_with_object():

sub init_with_object
{
my($self, $object) = @_;

my $choices = ...get $choices from $object somehow...

$self->field('my_field')->choices($choices);

# Proceed with normal init_with_object() stuff...
$self->SUPER::init_with_object($object);
}

You could also add the radio button group field in init_with_object()
if it doesn't exist, or set the choices if it does:

sub init_with_object
{
my($self, $object) = @_;

my $choices = ...get $choices from $object somehow...

# Set choices if the field already exists, otherwise add the field
if(my $field = $self->field('my_field'))
{
$self->field('my_field')->choices($choices);
}
else
{
$self->add_field(my_field =>
{
type => 'radio group',
choices => $choices,
});
}

# Proceed with normal init_with_object() stuff...
$self->SUPER::init_with_object($object);
}

-John

jfrm

unread,
May 24, 2012, 4:42:24 AM5/24/12
to rose-htm...@googlegroups.com

> There's no reason you can't do your form manipulations in
> init_with_object().  Just have your radio button group field
> ("my_field") all ready and waiting in the form, then set the choices
> in init_with_object():


Just thought I'd post an addendum to this solution to help anyone after the same thing.  It works nicely but I noticed that having implemented it, updating the field didn't work.  In the build_form routine, choices was set to empty array and init_with_object set up the choices as John suggested.  The reason (I think) is that I was using object_from_form to update the database record and as the choices were still set to empty array, it didn't accept any values for updating. Therefore, you must also add the choices to the field at the beginning of object_from_form as well as init_with_object.

To avoid code repetition, you can create a new mini-method subroutine that only adds the choices.  To get this working with 'menu', do the same thing but set the options and labels fields.

Reply all
Reply to author
Forward
0 new messages