| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
|
| 5 |
use HTML::FormHandler::I18N; |
| 6 |
$ENV{LANGUAGE_HANDLE} = 'en_en'; |
| 7 |
|
| 8 |
use_ok('HTML::FormHandler'); |
| 9 |
|
| 10 |
{ |
| 11 |
|
| 12 |
package My::Form; |
| 13 |
use HTML::FormHandler::Moose; |
| 14 |
extends 'HTML::FormHandler'; |
| 15 |
|
| 16 |
has '+name' => ( default => 'testform_' ); |
| 17 |
has '+no_preload' => ( default => 1 ); |
| 18 |
has_field 'optname' => ( temp => 'First' ); |
| 19 |
has_field 'reqname' => ( required => 1 ); |
| 20 |
has_field 'somename'; |
| 21 |
has_field 'my_selected' => ( type => 'Checkbox' ); |
| 22 |
has_field 'must_select' => ( type => 'Checkbox', required => 1 ); |
| 23 |
|
| 24 |
sub field_list |
| 25 |
{ |
| 26 |
return [ |
| 27 |
fruit => 'Select', |
| 28 |
optname => { temp => 'Second' } |
| 29 |
]; |
| 30 |
} |
| 31 |
|
| 32 |
sub options_fruit |
| 33 |
{ |
| 34 |
return ( |
| 35 |
1 => 'apples', |
| 36 |
2 => 'oranges', |
| 37 |
3 => 'kiwi', |
| 38 |
); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
my $form = My::Form->new; |
| 43 |
|
| 44 |
is( $form->field('optname')->temp, 'Second', 'got second optname field' ); |
| 45 |
|
| 46 |
ok( !$form->process, 'Empty data' ); |
| 47 |
is_deeply( $form->value, {}, 'no values returns hashref'); |
| 48 |
|
| 49 |
my $good = { |
| 50 |
reqname => 'hello', |
| 51 |
optname => 'not req', |
| 52 |
fruit => 2, |
| 53 |
must_select => 1, |
| 54 |
}; |
| 55 |
|
| 56 |
ok( $form->process($good), 'Good data' ); |
| 57 |
is( $form->field('somename')->value, undef, 'no value for somename' ); |
| 58 |
ok( !$form->field('somename')->has_value, 'predicate no value' ); |
| 59 |
$good->{somename} = 'testing'; |
| 60 |
$form->process($good); |
| 61 |
is( $form->field('somename')->value, 'testing', 'use input for extra data' ); |
| 62 |
is( $form->field('my_selected')->value, 0, 'correct value for unselected checkbox' ); |
| 63 |
|
| 64 |
ok( !$form->process( {} ), 'form doesn\'t validate with empty params' ); |
| 65 |
is( $form->num_errors, 0, 'form doesn\'t have errors with empty params' ); |
| 66 |
|
| 67 |
my $bad_1 = { |
| 68 |
reqname => '', |
| 69 |
optname => 'not req', |
| 70 |
fruit => 4, |
| 71 |
}; |
| 72 |
|
| 73 |
ok( !$form->process($bad_1), 'bad 1' ); |
| 74 |
ok( $form->field('fruit')->has_errors, 'fruit has error' ); |
| 75 |
ok( $form->field('reqname')->has_errors, 'reqname has error' ); |
| 76 |
ok( $form->field('must_select')->has_errors, 'must_select has error' ); |
| 77 |
ok( !$form->field('optname')->has_errors, 'optname has no error' ); |
| 78 |
is( $form->field('fruit')->id, "fruit", 'field has id' ); |
| 79 |
is( $form->field('fruit')->label, 'Fruit', 'field label' ); |
| 80 |
|
| 81 |
ok( !$form->process( {} ), 'no leftover params' ); |
| 82 |
is( $form->num_errors, 0, 'no leftover errors' ); |
| 83 |
ok( !$form->field('reqname')->has_errors, 'no leftover error in field' ); |
| 84 |
ok( !$form->field('optname')->fif, 'no lefover fif values' ); |
| 85 |
|
| 86 |
my $init_object = { |
| 87 |
reqname => 'Starting Perl', |
| 88 |
optname => 'Over Again' |
| 89 |
}; |
| 90 |
|
| 91 |
# if you're going to load something on new and expect it to work without |
| 92 |
# process, you need to have no_preload turned off |
| 93 |
$form = My::Form->new( init_object => $init_object, no_preload => 0 ); |
| 94 |
is( $form->field('optname')->value, 'Over Again', 'value with init_obj' ); |
| 95 |
# non-posted params |
| 96 |
$form->process( params => {} ); |
| 97 |
ok( !$form->validated, 'form validated' ); |
| 98 |
|
| 99 |
# it's not crystal clear what the behavior should be here, but I think |
| 100 |
# this is more correct than the previous behavior |
| 101 |
# it fills in the missing fields, which is what always happened for an |
| 102 |
# initial object (as opposed to hash), but it used to behave |
| 103 |
# differently for a hash, which seems wrong |
| 104 |
# TODO verify behavior is correct |
| 105 |
my $init_obj_plus_defaults = { |
| 106 |
'fruit' => undef, |
| 107 |
'must_select' => undef, |
| 108 |
'my_selected' => undef, |
| 109 |
'optname' => 'Over Again', |
| 110 |
'reqname' => 'Starting Perl', |
| 111 |
'somename' => undef, |
| 112 |
}; |
| 113 |
is_deeply( $form->value, $init_obj_plus_defaults, 'value with empty params' ); |
| 114 |
$init_object->{my_selected} = 0; # checkboxes must be forced to 0 |
| 115 |
$init_object->{must_select} = 0; |
| 116 |
my %fif = %$init_object; |
| 117 |
$fif{somename} = ''; |
| 118 |
$fif{fruit} = ''; |
| 119 |
$fif{must_select} = ''; |
| 120 |
$fif{my_selected} = ''; |
| 121 |
is_deeply( $form->fif, \%fif, 'get right fif with init_object' ); |
| 122 |
# make sure that checkbox is 0 in values |
| 123 |
$init_object->{must_select} = 1; |
| 124 |
$fif{must_select} = 1; |
| 125 |
ok( $form->process($init_object), 'form validates with params' ); |
| 126 |
#my %init_obj_value = (%$init_object, fruit => undef ); |
| 127 |
#is_deeply( $form->value, \%init_obj_value, 'value init obj' ); |
| 128 |
$init_object->{fruit} = undef; |
| 129 |
is_deeply( $form->value, $init_object, 'value init obj' ); |
| 130 |
$fif{must_select} = 1; |
| 131 |
$fif{my_selected} = 0; |
| 132 |
is_deeply( $form->fif, \%fif, 'get right fif with init_object' ); |
| 133 |
|
| 134 |
$form->clear; |
| 135 |
ok( !$form->has_value, 'Form value cleared' ); |
| 136 |
ok( !$form->has_input, 'Form input cleared' ); |
| 137 |
|
| 138 |
# check that form is cleared if fif is done before process |
| 139 |
$form->fif; |
| 140 |
$form->process($init_object); |
| 141 |
is_deeply( $form->fif, \%fif, 'get right fif when process preceded by fif'); |
| 142 |
|
| 143 |
$form = HTML::FormHandler->new( field_list => [ foo => { type => 'Text', required => 1 } ] ); |
| 144 |
|
| 145 |
if ( !$form->process( params => { bar => 1, } ) ) |
| 146 |
{ |
| 147 |
# On some versions, the above process() returned false, but |
| 148 |
# error_fields did not return anything. |
| 149 |
my @fields = $form->error_fields; |
| 150 |
if ( is( scalar @fields, 1, "there is an error field" ) ) |
| 151 |
{ |
| 152 |
my @errors = $fields[0]->all_errors; |
| 153 |
is( scalar @errors, 1, "there is an error" ); |
| 154 |
|
| 155 |
is( $errors[0], $fields[0]->label . " field is required", "error messages match" ); |
| 156 |
} |
| 157 |
else |
| 158 |
{ |
| 159 |
fail("there is an error"); |
| 160 |
fail("error messages match"); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
# 'image' input produces { foo => bar, 'foo.x' => 42, 'foo.y' => 23 } |
| 165 |
$form = HTML::FormHandler->new( name => 'baz', html_prefix => 1, field_list => [ 'foo' ] ); |
| 166 |
eval{ $form->process( params => { 'baz.foo' => 'bar', 'baz.foo.x' => 42, 'baz.foo.y' => 23 } ) }; |
| 167 |
ok( !$@, 'image field processed' ) or diag $@; |
| 168 |
is_deeply( $form->field( 'foo' )->value, { '' => 'bar', x => 42, y => 23 }, 'image field' ); |
| 169 |
|
| 170 |
{ |
| 171 |
package Test::Form; |
| 172 |
use HTML::FormHandler::Moose; |
| 173 |
extends 'HTML::FormHandler'; |
| 174 |
|
| 175 |
has_field 'foo'; |
| 176 |
has_field 'bar'; |
| 177 |
|
| 178 |
sub validate { |
| 179 |
my $self = shift; |
| 180 |
if( $self->field('foo')->value eq 'cow' ) { |
| 181 |
$self->field('foo')->value('bovine'); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
$form = Test::Form->new; |
| 186 |
$form->process( { foo => 'cow', bar => 'horse' } ); |
| 187 |
is_deeply( $form->value, { foo => 'bovine', bar => 'horse' }, 'correct value' ); |
| 188 |
|
| 189 |
# check for hashref constructor |
| 190 |
$form = HTML::FormHandler->new( { name => 'test_form', field_list => { one => 'Text', two => 'Text' } } ); |
| 191 |
ok( $form, 'form constructed ok' ); |
| 192 |
|
| 193 |
|
| 194 |
done_testing; |