Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Customised choices in form element?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
John Siracusa  
View profile  
 More options May 3 2009, 8:20 am
From: John Siracusa <sirac...@gmail.com>
Date: Sun, 3 May 2009 08:20:55 -0400
Local: Sun, May 3 2009 8:20 am
Subject: Re: Customised choices in form element?

On Sun, May 3, 2009 at 4:41 AM, James Masters <ja...@mastersgames.com> wrote:
> 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?

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.