[html-formfu] Populating $form_input with 'select' element options?

J. Bobby Lopez jbl at jbldata.com
Tue Nov 8 17:45:46 GMT 2011


I went ahead and ripped out $query->Vars() wherever possible on your
suggestion.  I have a fair handle now on populating the multi-select
fields, using $form->get_field() to focus specifically on a single
field.

I'm using the following to populate the multi-select, which _is_ working:

    $field->options($array_ref);

Though, I still haven't been able to highlight individual options as selected:

    $field->default($array_ref); # not working, but should be
    $form->default_values({ EmailDL => [ 'myvalue1', 'myvalue2'] }); #
not working, but should be

I am however, able to set 'selected' on the overall <select> tag, which is odd:

    $field->options($array_ref)->{attributes}->{selected} = 'selected';

..which generates this in HTML source:
<select name="EmailDL" id="EmailDL" multiple="multiple" selected="selected">

Although not what exactly what I want, it's helped my sanity somewhat  :)

looked at 'select_multi_value.t' and 'select_multi_default_values.t'
to make sure the syntax was right, but still no joy with setting
'selected' on individual options.

Likely I'm just doing something incorrectly, but no idea what.
Doesn't seem like it should be too complicated :\

Thanks again,
-Bobby

On Tue, Nov 8, 2011 at 4:44 AM, Carl Franks <fireartist at gmail.com> wrote:
> On 7 November 2011 21:35, J. Bobby Lopez <jbl at jbldata.com> wrote:
>> So I think I'm pretty close to what I'm heading for.   Here's what I'm doing:
>>
>> my $field_EmailDL = $form_insert_data->get_field( { name => 'EmailDL'} );
>> $field_EmailDL->options($emailDL_array);
>>
>> $emailDL_array contains the following:
>>
>> $VAR1 = [
>>          [
>>            'label',
>>            'mylabel1',
>>            'value',
>>            'myvalue1'
>>          ],
>>          [
>>            'label',
>>            'mylabel2',
>>            'value',
>>            'myvalue2'
>>          ]
>>        ];
>>
>> Now I realize that setting ->options() will essentially overwrite the
>> value of the 'EmailDL'.  That's fine, I fix that up later.
>>
>> What I'm trying to do now ( haven't found it in the docs ) is the way
>> to set one or more of the values in $emailDL_array to be selected.
>>
>>
>> The docs say:
>> "Its arguments must be an array-ref of items. Each item may be an
>> array ref of the form [ $value, $label ] or a hash-ref of the form {
>> value => $value, label => $label }. Each hash-ref may also have an
>> attributes key."
>>
>> Can "selected" be one of those attributes?  How would I specify it?
>>
>> Based on the above, I've tried the following which doesn't work:
>> { value => $value, label => $label, attributes => {selected => 'selected'} }
>>
>> Should I be looking at another function altogether for setting 'selected' items?
>
> No, don't pass 'selected' to options.
>
> Either do this:
>    $form->default_values( { EmailDL => \@default_values } );
> or this:
>    $field->default( \@default_values );
>
> This will ensure the default values are selected when the form is
> first displayed, but not if the form's displayed again after an
> invalid submission.
>
> Carl
>
> _______________________________________________
> HTML-FormFu mailing list
> HTML-FormFu at lists.scsys.co.uk
> http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu
>



More information about the HTML-FormFu mailing list