[html-formfu] How do I set selected option programatically?
Carl Franks
fireartist at gmail.com
Thu May 21 09:43:25 GMT 2009
2009/5/15 Dennis Daupert <ddaupert at gmail.com>:
> I found the documentation on setting a Select default,
> but not on how to set 'selected' on a select option
> on the fly.
>
> My yml config file configures three selects:
>
> =====================
> ---
> # indicator is the field that is used to test for form submission
> indicator: submit
> # Start listing the form elements
> auto_fieldset:
> legend: Add Entry
> elements:
> - type: Select
> name: project_id
> label: 'Projects (optional)'
> empty_first: 1
> model_config:
> resultset: Projects
> condition:
> active: 1
> - type: Select
> name: status_id
> label: Status
> empty_first: 1
> model_config:
> resultset: Statuses
> condition:
> active: 1
> - type: Select
> name: assignee
> label: Assignee
> empty_first: 1
> model_config:
> resultset: Users
> condition:
> active: 1
> =====================
>
> I need to set options on the fly for those selects.
>
> In my Catalyst app, I have this code (attempting to select the
> option thru the default method. If this should work, I must
> have the format wrong.):
>
> # Pre-populate selects
> my $fieldset = $form->get_element({ type => 'Fieldset' });
>
> my $proj_select = $fieldset->get_element({type => 'Select',
> name => 'project_id'});
> $proj_select->default($project_id) if $project_id;
>
> my $stat_select = $fieldset->get_element({type => 'Select',
> name => 'status_id'});
> $stat_select->default($status_id) if $status_id;
>
> my $assign_select = $fieldset->get_element({type => 'Select',
> name => 'assignee'});
> $assign_select->default($assignee) if $assignee;
>
> I do have confirmation that the variables are populated:
> $project_id, $status_id, $assignee.
I'm not sure what you mean by 'on the fly', but, yes,
$field->default() should do this.
The most likely thing that comes to mind, is that this is intended for
use *before* the form is submitted for the first time.
That means $form->submitted() should be false - could that be the problem?
Also, just a minor thing - use $form->get_field().
Trying to use get_element() for this kind of thing just gets messy.
Carl
More information about the HTML-FormFu
mailing list