File: compound.t

package info (click to toggle)
libhtml-formhandler-perl 0.40067-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,432 kB
  • ctags: 697
  • sloc: perl: 9,312; makefile: 2
file content (91 lines) | stat: -rw-r--r-- 3,023 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use strict;
use warnings;
use Test::More;
use HTML::FormHandler::Test;

{
    package MyApp::Form::Theme::Basic;
    use Moose::Role;

    sub build_do_form_wrapper {1}
    sub build_form_tags {{  wrapper_tag => 'div' }}
    sub build_update_subfields {{
        all => { tags => { label_tag => 'span' } },
        by_flag => { 'compound' => { do_wrapper => 1, do_label => 1,
           tags => {  wrapper_tag => 'span' }}},
    }}
    sub html_attributes {
        my ( $self, $field, $type, $attr ) = @_;
        $attr->{class} = ['frm', 'ele'] if $type eq 'element';
        $attr->{class} = ['frm', 'lbl'] if $type eq 'label';
        $attr->{class} = ['frm', 'wrp'] if $type eq 'wrapper';
        return $attr;
    }
}
{
    package MyApp::Form;
    use HTML::FormHandler::Moose;
    extends 'HTML::FormHandler';
    with 'MyApp::Form::Theme::Basic';

    has_field 'my_comp' => ( type => 'Compound' );
    has_field 'my_comp.one';
    has_field 'my_comp.two';
    has_field 'my_text' => ( type => 'Text' );
}

my $form = MyApp::Form->new;
my $rendered = $form->field('my_comp')->render;
my $expected =
'<span class="frm wrp" id="my_comp">
  <span class="frm lbl">My comp</span>
  <div class="frm wrp">
    <span class="frm lbl">One</span>
    <input class="frm ele" type="text" name="my_comp.one" id="my_comp.one" value="" />
  </div>
  <div class="frm wrp">
    <span class="frm lbl">Two</span>
    <input class="frm ele" type="text" name="my_comp.two" id="my_comp.two" value="" />
  </div>
</span>';
is_html( $rendered, $expected, 'compound rendered ok' );

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

    sub build_update_subfields {{ all => { tags => { label_after => ': ' }}}}
    has_field 'start_date' => ( type => 'DateTime', do_wrapper => 1, do_label => 1,
        tags => { wrapper_tag => 'fieldset' }, wrapper_class => 'start_date' );
    has_field 'start_date.month' => ( type => 'Integer', range_start => 1,
        range_end => 12 );
    has_field 'start_date.day' => ( type => 'Integer', range_start => 1,
        range_end => 31 );
    has_field 'start_date.year' => ( type => 'Integer', range_start => 2000,
        range_end => 2020 );
}

$form = Test::DT->new;
my $params = { 'start_date.month' => 7, 'start_date.day' => 14, 'start_date.year' => '2006' };
$form->process( $params );

$rendered = $form->field('start_date')->render;
is_html( $rendered,
'<fieldset class="start_date" id="start_date"><legend>Start date</legend>
  <div>
    <label for="start_date.month">Month: </label>
    <input type="text" name="start_date.month" id="start_date.month" size="8" value="7" />
  </div>
  <div>
    <label for="start_date.day">Day: </label>
    <input type="text" name="start_date.day" id="start_date.day" size="8" value="14" />
  </div>
  <div>
    <label for="start_date.year">Year: </label>
    <input type="text" name="start_date.year" id="start_date.year" size="8" value="2006" />
  </div>
</fieldset>',
   'output from DateTime' );

done_testing;