Debian Codegraph @

t/infl_defl/infl_defl.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4
5 {
6 # This form takes an array from the init_object and flattens it
7 # into a string for displaying in a form;
8 # When the string is submitted from the form, it's then
9 # inflated into an array for validation and for returning
10 # in the 'value' hashref.#
11 {
12 package Test::Deflate1;
13 use HTML::FormHandler::Moose;
14 extends 'HTML::FormHandler';
15
16 has_field 'bullets' => ( type => 'Text',
17 inflate_method => \&string_to_array,
18 validate_method => \&check_bullets,
19 deflate_method => \&array_to_string,
20 );
21 sub array_to_string {
22 my ( $self, $value ) = @_;
23 my $string = '';
24 my $sep = '';
25 for ( @$value ) {
26 $string .= $sep . $_->{text};
27 $sep = ';';
28 }
29 return $string;
30 }
31 sub check_bullets {
32 my $self = shift;
33 my $value = $self->value;
34 unless( ref $value eq 'HASH' && scalar @$value == 3 ) {
35 $self->add_error('bullets not valid');
36 }
37 }
38 sub string_to_array {
39 my ( $self, $value ) = @_;
40 return [ map { { text => $_ } } split(/\s*;\s*/, $value) ];
41 }
42 }
43
44 my $init_object = { bullets => [{ text => 'one'}, { text => 'two' }, { text => 'three'}] };
45 my $fif = { bullets => 'one;two;three' };
46 my $form = Test::Deflate1->new;
47 ok( $form, 'form built');
48 $form->process( init_object => $init_object, params => {} );
49 is_deeply( $form->fif, $fif, 'right fif' );
50 is_deeply( $form->value, $init_object, 'right value' );
51
52 $form->process( params => $fif );
53 is_deeply( $form->fif, $fif, 'right fif' );
54 is_deeply( $form->value, $init_object, 'right value' );
55 }
56
57 done_testing;