| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
|
| 5 |
# this test emulates using a database item, and checks |
| 6 |
# to see that the repeatable is updated from the item, |
| 7 |
# and that 'fif' is correct |
| 8 |
{ |
| 9 |
package MyApp::Test::Form; |
| 10 |
use HTML::FormHandler::Moose; |
| 11 |
extends 'HTML::FormHandler'; |
| 12 |
|
| 13 |
has_field 'foo'; |
| 14 |
has_field 'bar'; |
| 15 |
has_field 'my_rep' => ( type => 'Repeatable' ); |
| 16 |
has_field 'my_rep.rep_id' => ( type => 'PrimaryKey' ); |
| 17 |
has_field 'my_rep.one'; |
| 18 |
has_field 'my_rep.two'; |
| 19 |
|
| 20 |
sub update_model { |
| 21 |
my $self = shift; |
| 22 |
my $value = $self->value; |
| 23 |
my $index = 1; |
| 24 |
foreach my $rep ( @{ $value->{my_rep} } ) { |
| 25 |
$rep->{rep_id} = $index; |
| 26 |
$index++; |
| 27 |
} |
| 28 |
$self->item( $value ); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
my $form = MyApp::Test::Form->new; |
| 33 |
ok( $form ); |
| 34 |
|
| 35 |
my $item = { |
| 36 |
foo => 'my_foo', |
| 37 |
bar => 'my_bar', |
| 38 |
my_rep => [ |
| 39 |
], |
| 40 |
}; |
| 41 |
$form->process( item => $item ); |
| 42 |
my $fif = $form->fif; |
| 43 |
$fif->{'my_rep.0.one'} = 'my_one'; |
| 44 |
$fif->{'my_rep.0.two'} = 'my_two'; |
| 45 |
$form->process( item => $item, params => $fif ); |
| 46 |
$fif->{'my_rep.0.rep_id'} = 1; |
| 47 |
my $new_fif = $form->fif; |
| 48 |
is_deeply( $new_fif, $fif, 'fif is correct' ); |
| 49 |
is( $form->field('my_rep.0.rep_id')->value, 1, 'pk has correct value' ); |
| 50 |
|
| 51 |
done_testing; |