What a wonderfully useful tip. Thank you. I didn't understand why I was told to put my Model in the stash as it seemed to make no difference. Now I do. This is much easier than the long, coded way. However, how can I specify which column I want to use in the table? My lookup table contains a few different columns and the Select element is populated using data from the wrong column. Is there some way to specify which one I want?

Jens Schwarz wrote:
Hi,

some weeks ago Carl gave me this useful hint (see below) for populating Select 
elements. Works fine with Select elements. Now I tried to adopt this for my 
ComboBox but failed (just think of the example below being a ComboBox instead 
of a Select): The result is that when I call the edit action in my Catalyst 
controller, the genre saved in the recordset is displayed in the genres_text 
part of the ComboBox - even if the genre would be available in genres_select.

Do I have to handle ComboBoxes in another way than Selects to populate them?

2008/12/4 Jens Schwarz <[email protected]>:
   my @genreobj = $c->model('My::Genres')->all();
   my @genres;
   foreach my $g (@genreobj) {
     push(@genres, [$g->id, $g->name]);
   }
   my $select = $form->get_element({name => 'genres'});
   $select->options(\...@genres);
BTW, you could get rid of all that code to populate $select->options()
by making sure the appropriate Model is in your form stash, by setting
this in your cat application config:

    'Controller::HTML::FormFu':
      model_stash:
        schema: My

Then modifying the Select field in your form config:

    type: Select
    name: genres
    model_config:
        resultset: Genres

That will also have the benefit of populating the Select menu every
time the form's built - so if you add an AutoSet constraint to the
field, it'll check whether the user-submitted value was a valid
option, when you call $form->submitted_and_valid().

Carl

_______________________________________________
HTML-FormFu mailing list
[email protected]
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu

Reply via email to