Permalink
Browse files

call inflate_default on repeatable elements

  • Loading branch information...
gshank committed Oct 13, 2012
1 parent 9aca6bd commit 56c1c2b80d7bfff2bddf78d05897fd2c15cb2b66
Showing with 30 additions and 1 deletion.
  1. +4 −1 lib/HTML/FormHandler/Field/Repeatable.pm
  2. +26 −0 t/infl_defl/comp_field.t
@@ -301,6 +301,9 @@ sub _result_from_object {
my $field = $self->clone_element($index);
my $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 );
push @new_values, $result->value;
$self->add_field($field);
@@ -344,7 +347,7 @@ sub add_extra {
$self->index($index);
}
-# create an empty form
+# create an empty field
sub _result_from_fields {
my ( $self, $result ) = @_;
View
@@ -70,4 +70,30 @@ is_deeply( $form->fif, $fif, 'right 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' );
+{
+ 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;

0 comments on commit 56c1c2b

Please sign in to comment.