| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
|
| 5 |
{ |
| 6 |
package Test::User::Repeatable; |
| 7 |
use HTML::FormHandler::Moose; |
| 8 |
extends 'HTML::FormHandler'; |
| 9 |
|
| 10 |
has_field 'user_name'; |
| 11 |
has_field 'occupation'; |
| 12 |
has_field 'employers' => ( type => 'Repeatable', num_extra => 1 ); |
| 13 |
has_field 'employers.employer_id' => ( type => 'PrimaryKey' ); |
| 14 |
has_field 'employers.name'; |
| 15 |
has_field 'employers.address'; |
| 16 |
} |
| 17 |
my $form = Test::User::Repeatable->new; |
| 18 |
my $unemployed_params = { |
| 19 |
user_name => "No Employer", |
| 20 |
occupation => "Unemployed", |
| 21 |
'employers.0.employer_id' => '', # empty string |
| 22 |
'employers.0.name' => '', |
| 23 |
'employers.0.address' => '' |
| 24 |
}; |
| 25 |
$form->process( $unemployed_params); |
| 26 |
ok( $form->validated, "User with empty employer validates" ); |
| 27 |
is_deeply( $form->value, { employers => [], user_name => 'No Employer', occupation => 'Unemployed' }, |
| 28 |
'creates right value for empty repeatable' ); |
| 29 |
is_deeply( $form->fif, $unemployed_params, 'right fif for empty repeatable' ); |
| 30 |
$form->field('employers')->add_extra; |
| 31 |
my $expected_fif = { |
| 32 |
'employers.0.address' => '', |
| 33 |
'employers.0.employer_id' => '', |
| 34 |
'employers.0.name' => '', |
| 35 |
'employers.1.address' => '', |
| 36 |
'employers.1.employer_id' => '', |
| 37 |
'employers.1.name' => '', |
| 38 |
'occupation' => 'Unemployed', |
| 39 |
'user_name' => 'No Employer', |
| 40 |
}; |
| 41 |
|
| 42 |
is_deeply( $form->fif, $expected_fif, 'fif is correct with additional element' ); |
| 43 |
|
| 44 |
my $obj = { |
| 45 |
occupation => 'Flaneur', |
| 46 |
user_name => 'billy', |
| 47 |
employers => [ |
| 48 |
{ name => 'First Employer', address => '', employer_id => 1 }, |
| 49 |
{ name => 'Second Employer', address => '', employer_id => 2 }, |
| 50 |
], |
| 51 |
}; |
| 52 |
|
| 53 |
$form->process( init_object => $obj ); |
| 54 |
$expected_fif = { |
| 55 |
'occupation' => 'Flaneur', |
| 56 |
'user_name' => 'billy', |
| 57 |
'employers.0.address' => '', |
| 58 |
'employers.0.employer_id' => 1, |
| 59 |
'employers.0.name' => 'First Employer', |
| 60 |
'employers.1.address' => '', |
| 61 |
'employers.1.employer_id' => 2, |
| 62 |
'employers.1.name' => 'Second Employer', |
| 63 |
'employers.2.address' => '', |
| 64 |
'employers.2.employer_id' => '', |
| 65 |
'employers.2.name' => '', |
| 66 |
}; |
| 67 |
is_deeply( $form->fif, $expected_fif, 'fif is correct with num_extra' ); |
| 68 |
|
| 69 |
done_testing; |