[html-formfu] Programmatically changing a
HTML::FormFu::Element::Select option list
Carl Franks
fireartist at gmail.com
Wed Oct 31 08:58:17 GMT 2007
On 31/10/2007, Scott Wall-gmail <tswall at gmail.com> wrote:
> I apologize if the answer to the question is obvious as I am just
> acquainting myself with the Catalyst framework and Perl in general. I am a
> java and ruby developer by background. How does one programmatically access
> the list of options in a HTML::FormFu::Element::Select option list? As a
> form is loaded via $form->load_config_file( $my_yml_file ) I'd like to set a
> given option as selected based on data pulled from the database, but the
> option list appears to be a nested arrayref that I can't seem to manipulate.
>
> I have done this with other elements, for example
> HTML::FormFu::Element::Text elements as follows:
>
> my $textbox_elements = $form->get_elements({type => 'Text' });
> foreach my $tb (@$textbox_elements) {
> my $element_name = $tb->{name};
>
> my $sf = get_some_object_from_the_database();
> $tb->attributes->{value} = $sf->finding_value;
> }
>
> Any assistance would be wonderful.
Hi Scott,
Welcome - and I hope you find your dip into perl an enjoyable experience!
The correct way to mark an option as selected is:
$field->default( $value );
This will automatically add selected="selected" to the option item
matching $value.
default() is also the correct way to do this for all fields, rather
than using attributes().
I'll make myself a note to look at the docs, and try to make this more clear.
Cheers,
Carl
More information about the HTML-FormFu
mailing list