Skip to content

Commit

Permalink
call inflate_default on repeatable elements
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank committed Oct 13, 2012
1 parent 9aca6bd commit 56c1c2b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/HTML/FormHandler/Field/Repeatable.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ sub _result_from_object {
my $field = $self->clone_element($index); my $field = $self->clone_element($index);
my $result = my $result =
HTML::FormHandler::Field::Result->new( name => $index, parent => $self->result ); HTML::FormHandler::Field::Result->new( name => $index, parent => $self->result );
if( $field->has_inflate_default_method ) {
$element = $field->inflate_default($element);
}
$result = $field->_result_from_object( $result, $element ); $result = $field->_result_from_object( $result, $element );
push @new_values, $result->value; push @new_values, $result->value;
$self->add_field($field); $self->add_field($field);
Expand Down Expand Up @@ -344,7 +347,7 @@ sub add_extra {
$self->index($index); $self->index($index);
} }


# create an empty form # create an empty field
sub _result_from_fields { sub _result_from_fields {
my ( $self, $result ) = @_; my ( $self, $result ) = @_;


Expand Down
26 changes: 26 additions & 0 deletions t/infl_defl/comp_field.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -70,4 +70,30 @@ is_deeply( $form->fif, $fif, 'right fif' );
is( $form->field('foo.one')->fif, 'x', 'correct fif' ); is( $form->field('foo.one')->fif, 'x', 'correct fif' );
is( $form->field('foo')->value, 'one-x-two-xx-three-xxx', 'right value for foo field' ); is( $form->field('foo')->value, 'one-x-two-xx-three-xxx', 'right value for foo field' );


{
package Test::RepDeflate;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';

has_field 'foo' => ( type => 'Repeatable' );
has_field 'foo.contains' => ( type => '+Test::Field' );
has_field 'bar';
sub validate_foo {
my ( $self, $field ) = @_;
my $value = $field->value;
unless ( ref $value eq 'ARRAY' ) {
$self->add_error('wrong value');
}
}
}

$form = Test::RepDeflate->new;
$init_object = { foo => ['one-1-two-2-three-3', 'one-10-two-11-three-12'], bar => 'xxyyzz' };
$form->process( init_object => $init_object, params => {} );
is_deeply( $form->value, { foo => [ { one => 1, two => 2, three => 3 }, { one => 10, two => 11, three => 12 } ],
bar => 'xxyyzz' }, 'value is correct?' );
is_deeply( $form->fif, { 'foo.0.one' => 1, 'foo.0.two' => 2, 'foo.0.three' => 3,
'foo.1.one' => 10, 'foo.1.two' => 11, 'foo.1.three' => 12, bar => 'xxyyzz' },
'fif is correct' );

done_testing; done_testing;

0 comments on commit 56c1c2b

Please sign in to comment.