| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
|
| 5 |
# This demonstrates a field that comes in as a single string from |
| 6 |
# defaults/init_object/item, and is split up into multiple subfields, |
| 7 |
# then is flattened again into a string. |
| 8 |
# |
| 9 |
# For this case, the meaning of the words "deflate" and "inflate" seems swapped. |
| 10 |
# In general, 'inflate' means "turn into some form that can be validated" and |
| 11 |
# 'deflate' means "turn into some form that can be presented in a form |
| 12 |
# |
| 13 |
# When inflations are involved, the format of the field value is not necessarily the |
| 14 |
# same when it is validated and when it is returned after successful processing |
| 15 |
|
| 16 |
{ |
| 17 |
package Test::Field; |
| 18 |
use HTML::FormHandler::Moose; |
| 19 |
extends 'HTML::FormHandler::Field::Compound'; |
| 20 |
|
| 21 |
has_field 'one'; |
| 22 |
has_field 'two'; |
| 23 |
has_field 'three'; |
| 24 |
has '+inflate_default_method' => ( default => sub { \&inflate_default_field } ); |
| 25 |
has '+deflate_value_method' => ( default => sub { \&deflate_field } ); |
| 26 |
|
| 27 |
sub inflate_default_field { |
| 28 |
my ( $self, $value ) = @_; |
| 29 |
my %hash = split(/-/, $value); |
| 30 |
return \%hash; |
| 31 |
} |
| 32 |
sub deflate_field { |
| 33 |
my ( $self, $value ) = @_; |
| 34 |
my $string = 'one-' . $value->{one}; |
| 35 |
$string .= '-two-' . $value->{two}; |
| 36 |
$string .= '-three-' . $value->{three}; |
| 37 |
return $string; |
| 38 |
} |
| 39 |
} |
| 40 |
{ |
| 41 |
package Test::Deflate; |
| 42 |
use HTML::FormHandler::Moose; |
| 43 |
extends 'HTML::FormHandler'; |
| 44 |
|
| 45 |
has_field 'foo' => ( type => '+Test::Field' ); |
| 46 |
has_field 'bar'; |
| 47 |
sub validate_foo { |
| 48 |
my ( $self, $field ) = @_; |
| 49 |
my $value = $field->value; |
| 50 |
unless ( ref $value eq 'HASH' ) { |
| 51 |
$self->add_error('wrong value'); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
my $form = Test::Deflate->new; |
| 57 |
ok( $form, 'form builds' ); |
| 58 |
my $init_object = { foo => 'one-1-two-2-three-3', bar => 'xxyyzz' }; |
| 59 |
$form->process( init_object => $init_object, params => {} ); |
| 60 |
is_deeply( $form->value, { foo => { one => 1, two => 2, three => 3 }, |
| 61 |
bar => 'xxyyzz' }, 'value is correct?' ); |
| 62 |
is_deeply( $form->fif, { 'foo.one' => 1, 'foo.two' => 2, 'foo.three' => 3, bar => 'xxyyzz' }, |
| 63 |
'fif is correct' ); |
| 64 |
|
| 65 |
my $fif = { bar => 'aabbcc', 'foo.one' => 'x', 'foo.two' => 'xx', 'foo.three' => 'xxx' }; |
| 66 |
$form->process( params => $fif ); |
| 67 |
ok( $form->validated, 'form validated' ); |
| 68 |
is_deeply( $form->value, { bar => 'aabbcc', foo => 'one-x-two-xx-three-xxx' }, 'right value' ); |
| 69 |
is_deeply( $form->fif, $fif, 'right fif' ); |
| 70 |
is( $form->field('foo.one')->fif, 'x', 'correct fif' ); |
| 71 |
is( $form->field('foo')->value, 'one-x-two-xx-three-xxx', 'right value for foo field' ); |
| 72 |
|
| 73 |
{ |
| 74 |
package Test::RepDeflate; |
| 75 |
use HTML::FormHandler::Moose; |
| 76 |
extends 'HTML::FormHandler'; |
| 77 |
|
| 78 |
has_field 'foo' => ( type => 'Repeatable' ); |
| 79 |
has_field 'foo.contains' => ( type => '+Test::Field' ); |
| 80 |
has_field 'bar'; |
| 81 |
sub validate_foo { |
| 82 |
my ( $self, $field ) = @_; |
| 83 |
my $value = $field->value; |
| 84 |
unless ( ref $value eq 'ARRAY' ) { |
| 85 |
$self->add_error('wrong value'); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
$form = Test::RepDeflate->new; |
| 91 |
$init_object = { foo => ['one-1-two-2-three-3', 'one-10-two-11-three-12'], bar => 'xxyyzz' }; |
| 92 |
$form->process( init_object => $init_object, params => {} ); |
| 93 |
is_deeply( $form->value, { foo => [ { one => 1, two => 2, three => 3 }, { one => 10, two => 11, three => 12 } ], |
| 94 |
bar => 'xxyyzz' }, 'value is correct?' ); |
| 95 |
is_deeply( $form->fif, { 'foo.0.one' => 1, 'foo.0.two' => 2, 'foo.0.three' => 3, |
| 96 |
'foo.1.one' => 10, 'foo.1.two' => 11, 'foo.1.three' => 12, bar => 'xxyyzz' }, |
| 97 |
'fif is correct' ); |
| 98 |
|
| 99 |
done_testing; |