PageRenderTime 17ms CodeModel.GetById 2ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/xt/multiple_forms.t

http://github.com/gshank/html-formhandler
Unknown | 76 lines | 63 code | 13 blank | 0 comment | 0 complexity | 7ace6c23be4b1242dfce4398e88b56cc MD5 | raw file
 1use Test::More tests => 9;
 2
 3
 4use_ok( 'HTML::FormHandler' );
 5
 6{
 7   package My::Form::One;
 8   use HTML::FormHandler::Moose;
 9
10   extends 'HTML::FormHandler';
11
12   # this form specifies the form name
13   has '+name' => ( default => 'One' );
14   has '+html_prefix' => ( default => 1 );
15
16   has_field 'field_one';
17   has_field 'field_two';
18   has_field 'field_three';
19}
20my $form1 = My::Form::One->new;
21ok( $form1, 'get first form' );
22
23{
24   package My::Form::Two;
25   use Moose;
26   extends 'HTML::FormHandler';
27
28   # this form uses the default random form name generation
29   has '+html_prefix' => ( default => 1 );
30
31   sub field_list {
32       [
33            field_one => 'Text',
34            field_two => 'Text',
35            field_three => 'Text',
36       ]
37   }
38}
39my $form2 = My::Form::Two->new;
40ok( $form2, 'get second form' );
41
42my $params = {
43   'One.field_one' => 'First field in first form',
44   'One.field_two' => 'Second field in first form',
45   'One.field_three' => 'Third field in first form',
46   $form2->field('field_one')->html_name =>
47             'First field in second form',
48   $form2->field('field_two')->html_name =>
49              'Second field in second form',
50   $form2->field('field_three')->html_name =>
51              'Third field in second form',
52};
53$form1->process( $params );
54ok( $form1->validated, 'validated first form' );
55is( $form1->field('field_one')->value, 'First field in first form',
56   'value of field in first form is correct' );
57my $fif_params = $form1->fif;
58is_deeply( $fif_params, {
59   'One.field_one' => 'First field in first form',
60   'One.field_two' => 'Second field in first form',
61   'One.field_three' => 'Third field in first form',
62   }, 'fif params correct');
63
64$form2->process( $params );
65ok( $form2->validated, 'validated second form' );
66is( $form2->field('field_three')->value, 'Third field in second form',
67   'value of field in second form is correct' );
68
69$params = {
70   'One.field_one' => 'First field in first form',
71   'One.field_two' => 'Second field in first form',
72   'One.field_three' => 'Third field in first form',
73};
74$form2 = My::Form::Two->new( params => $params );
75ok( !$form2->has_params, 'has_params checks only params intented for the form');
76