The Perl and Raku Conference 2025: Greenville, South Carolina - June 27-29 Learn more

use strict;
{
package Test::Form;
extends 'HTML::FormHandler';
has '+name' => ( default => 'options_form' );
has_field 'test_field' => (
type => 'Text',
label => 'TEST',
id => 'f99',
);
has_field 'fruit' => ( type => 'Select' );
has_field 'vegetables' => ( type => 'Multiple', input_without_param => [], not_nullable => 1 );
has_field 'empty' => ( type => 'Multiple' );
sub init_value_fruit { 2 }
# the following sometimes happens with db options
sub options_empty { ([]) }
has 'options_fruit' => ( is => 'rw', traits => ['Array'],
default => sub { [1 => 'apples', 2 => 'oranges',
3 => 'kiwi'] } );
sub options_vegetables {
return (
1 => 'lettuce',
2 => 'broccoli',
3 => 'carrots',
4 => 'peas',
);
}
}
my $form = Test::Form->new;
ok( $form, 'create form');
my $veg_options = [ {'label' => 'lettuce',
'value' => 1 },
{'label' => 'broccoli',
'value' => 2 },
{'label' => 'carrots',
'value' => 3 },
{'label' => 'peas',
'value' => 4 } ];
my $field_options = $form->field('vegetables')->options;
is_deeply( $field_options, $veg_options,
'get options for vegetables' );
$field_options = $form->field('fruit')->options;
is_deeply( $field_options,
[ {'label' => 'apples',
'value' => 1 },
{'label' => 'oranges',
'value' => 2 },
{'label' => 'kiwi',
'value' => 3 } ],
'get options for fruit' );
my $params = {
fruit => 2,
vegetables => [2,4],
empty => '',
};
is( $form->field('fruit')->value, 2, 'initial value ok');
$form->process( $params );
ok( $form->validated, 'form validated' );
is( $form->field('fruit')->value, 2, 'fruit value is correct');
is_deeply( $form->field('vegetables')->value, [2,4], 'vegetables value is correct');
is_deeply( $form->fif, { fruit => 2, vegetables => [2, 4], test_field => '', empty => '' },
'fif is correct');
is_deeply( $form->values, { fruit => 2, vegetables => [2, 4], empty => undef },
'values are correct');
$params = {
fruit => 2,
};
$form->process($params);
is_deeply( $form->field('vegetables')->value, [], 'value for vegetables correct' );
done_testing;