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

J. Bobby Lopez jbl at jbldata.com
Mon Nov 7 21:35:03 GMT 2011


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?


Thanks again,
-Bobby

On Mon, Nov 7, 2011 at 1:18 PM, J. Bobby Lopez <jbl at jbldata.com> wrote:
> Thanks very much for this Carl, your insight pointed me in the right
> direction.  It seems there are some caveats in the way I've been
> reading CGI vars which affect multi-valued parameters.
>
> From http://perldoc.perl.org/CGI.html
> --
> When using this, the thing you must watch out for are multivalued CGI
> parameters. Because a hash cannot distinguish between scalar and list
> context, multivalued parameters will be returned as a packed string,
> separated by the "\0" (null) character. You must split this packed
> string in order to get at the individual values. This is the
> convention introduced long ago by Steve Brenner in his cgi-lib.pl
> module for Perl version 4.
> --
>
> So it may in fact be possible to use $form_input to populate a formfu
> object with multi-valued parameters, as long as the packed string with
> "\0" dealt with.
>
> Still, I may just do it they way you've suggested in order to keep
> things clear and obvious.
>
> -Bobby
>
>
> On Sat, Nov 5, 2011 at 6:35 PM, Carl Franks <fireartist at gmail.com> wrote:
>> On 4 November 2011 19:24, J. Bobby Lopez <jbl at jbldata.com> wrote:
>>> I also see that when I dump %cgivars after submitting a form with
>>> multiple values selected in a 'select' element, that the values become
>>> concatenated.
>>>
>>> E.g (FormFu yml):
>>>
>>>    - type: Select
>>>      name: EmailDL
>>>      label: 'Subscribed Lists:'
>>>      options:
>>>        - [ 'male', 'Male' ]
>>>        - [ 'female', 'Female' ]
>>>
>>>
>>> And resulting params (%cgivars):
>>>   {
>>>          ...,
>>>          'EmailDL' => 'malefemale',
>>>          ...,
>>>   }
>>>
>>>
>>> So I'm not sure how FormFu would deal with multi-valued selects in general?
>>
>> Hi,
>> I've just committed test files checking that multi-value selects work.
>> Setting default with $field->default(\@values), and submitting multiple values:
>> https://github.com/fireartist/HTML-FormFu/blob/master/t/elements/select_multi_value.t
>> Setting default with $form->default_values({ name => \@values });
>> https://github.com/fireartist/HTML-FormFu/blob/master/t/elements/select_multi_default_values.t
>> Everything seems to work as expected.
>>
>> I'm not sure where your %cgivars comes from - and whether you're using
>> CGI, CGI::Simple or Catalyst?
>>
>> 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