Debian Codegraph @

t/render/classes.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4 use HTML::FormHandler::Test;
5
6 {
7 package Test::Form::Theme;
8 use Moose::Role;
9
10 sub build_form_tags {{
11 form_tag => 1,
12 }}
13 sub build_update_subfields {{
14 all => { tags => { some_tag => 1, field_tag => 0 } },
15 foo => { element_class => ['interesting'] }
16 }}
17 }
18
19 {
20 package Test::Form;
21 use HTML::FormHandler::Moose;
22 extends 'HTML::FormHandler';
23 with 'Test::Form::Theme';
24
25 has_field 'foo' => ( element_class => ['fld_def'] );
26 has_field 'bar' => ( tags => { field_tag => 1 } );
27 has_field 'rox' => ( wrapper_class => 'frmwrp' );
28
29 }
30
31 my $form = Test::Form->new;
32 ok( $form );
33 my $element_class = $form->field('foo')->element_class;
34 is_deeply( $element_class, ['interesting', 'fld_def'], 'got both classes' );
35 is_deeply( $form->field('rox')->tags, { some_tag => 1, field_tag => 0 }, 'correct widget tags' );
36 is_deeply( $form->field('bar')->tags,
37 { some_tag => 1, field_tag => 1 }, 'correct widget tags' );
38
39 {
40 package Test::ClassForm;
41 use HTML::FormHandler::Moose;
42 extends 'HTML::FormHandler';
43
44 has '+name' => ( default => 'hfhform' );
45 sub build_update_subfields {{
46 all => { wrapper_class => 'hfh' }
47 }}
48 has_field 'foo';
49 has_field 'bar';
50 has_field 'nox';
51
52 }
53
54 $form = Test::ClassForm->new;
55 $form->process;
56 is_deeply( $form->field('foo')->wrapper_class, ['hfh'], 'foo has class' );
57 is_deeply( $form->field('bar')->wrapper_class, ['hfh'], 'bar has class' );
58 is_deeply( $form->field('nox')->wrapper_class, ['hfh'], 'nox has class' );
59 my $rendered = $form->render;
60 my $expected =
61 '<form id="hfhform" method="post">
62 <div class="form_messages"></div>
63 <div class="hfh">
64 <label for="foo">Foo</label>
65 <input type="text" id="foo" name="foo" value="">
66 </div>
67 <div class="hfh">
68 <label for="bar">Bar</label>
69 <input type="text" id="bar" name="bar" value="">
70 </div>
71 <div class="hfh">
72 <label for="nox">Nox</label>
73 <input type="text" id="nox" name="nox" value="">
74 </div>
75 </form>';
76 is_html( $rendered, $expected, 'rendered correctly' );
77
78 {
79 package Test::ElementClassField;
80 use HTML::FormHandler::Moose;
81 extends 'HTML::FormHandler::Field::Text';
82
83 sub build_element_class { ['foo'] }
84 }
85 {
86 package Test::ElementClassForm;
87 use HTML::FormHandler::Moose;
88 extends 'HTML::FormHandler';
89
90 has_field 'foo' => (
91 type => '+Test::ElementClassField',
92 element_class => 'bar',
93 );
94
95 # Note: all => { element_class => 'baz' } does not
96 # override the specific setting on the field. This is
97 # deliberate. The idea is that the more specific setting
98 # should override the general setting. Like all of these things,
99 # sometimes people are going to want it one way and sometimes the
100 # other. If you want to set the element_class for all elements
101 # at once, don't put a specific element_class on the field.
102 sub build_update_subfields {
103 {
104 foo => { element_class => 'baz' },
105 }
106 };
107 }
108 $form = Test::ElementClassForm->new;
109 $form->process;
110 is_deeply( $form->field('foo')->element_class, [qw( baz )], 'foo has correct classes' );
111
112 done_testing;