File: control_states.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 (260 lines) | stat: -rw-r--r-- 10,810 bytes parent folder | download | duplicates (3)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
use strict;
use warnings;
use Test::More;
use HTML::FormHandler::Test;
$ENV{LANGUAGE_HANDLE} = 'en_en';

{
    package MyApp::Form::CtrlStates::Theme;
    use Moose::Role;
    with 'HTML::FormHandler::Widget::Theme::Bootstrap';

    sub build_form_tags {{
        after_start => '<fieldset><legend>Form control states</legend>',
        before_end => '</fieldset>',
    }}

    sub build_update_subfields {{
        focusedInput => { element_class => ['input-xlarge', 'focused'] },
        uneditableInput => { element_class => ['input-xlarge', 'uneditable-input'] },
        disabledInput => { element_class => ['input-xlarge'],
            element_attr => { placeholder => 'Disabled input here…' } },
        optionsCheckbox2 => { element_class => ['checkbox'],
            option_label => 'This is a disabled checkbox' },
        inputError3 => { wrapper_class => ['success'],
            tags => { after_element => qq{\n<span class="help-inline">Woohoo!</span>} } },
        selectError => { wrapper_class => ['success'],
            tags => { after_element => qq{\n<span class="help-inline">Woohoo!</span>} } },
        form_actions => { do_wrapper => 1, do_label => 0 },
        'form_actions.save' => { widget_wrapper => 'None', element_class => ['btn', 'btn-primary'] },
        'form_actions.cancel' => { widget_wrapper => 'None', element_class => ['btn'] },
    }}
}

{
    package MyApp::Form::CtrlStates;
    use HTML::FormHandler::Moose;
    extends 'HTML::FormHandler';
    with 'MyApp::Form::CtrlStates::Theme';

    has '+name' => ( default => 'test_form' );
    has_field 'focusedInput' => ( default => 'This is focused…', label => 'Focused input' );
    has_field 'uneditableInput' => ( type => 'NonEditable', label => 'Uneditable input',
        value => 'Some value here',
    );
    has_field 'disabledInput' => ( disabled => 1, label => 'Disabled input' );
    has_field 'optionsCheckbox2' => ( type => 'Checkbox', checkbox_value => "option1",
        disabled => 1, label => 'Disabled checkbox' );
    has_field 'inputError1' => ( validate_method => \&validate_ie1,
        label => 'Input with warning' );
    has_field 'inputError2' => ( validate_method => \&validate_ie2,
        label => 'Input with error' );;
    has_field 'inputError3' => ( label => 'Input with success' );;
    has_field 'selectError' => ( type => 'Select', label => 'Select with success',
        options => [{ value => 1, label => '1' }, { value => 2, label => '2' },
            { value => 3, label => '3'},
            { value => 4, label => '4' }, { value => 5, label => '5' } ],
    );
    has_field 'form_actions' => ( type => 'Compound' );
    has_field 'form_actions.save' => ( widget => 'ButtonTag', type => 'Submit',
        value => 'Save changes' );
    has_field 'form_actions.cancel' => ( widget => 'ButtonTag', type => 'Reset',
        value => 'Cancel' );
    sub validate_ie1 {
        my $self = shift; # self is the field here
        $self->add_warning('Something may have gone wrong');
    }
    sub validate_ie2 {
        my $self = shift;
        $self->add_error('Please correct the error');
    }
}

my $form = MyApp::Form::CtrlStates->new;

my $params = $form->fif;
$params->{inputError1} = 'entry';
$params->{inputError2} = 'xxx';
$params->{inputError3} = 'success';
$form->process( $params );

my $expected =
'<div class="control-group">
  <label class="control-label" for="focusedInput">Focused input</label>
  <div class="controls">
    <input class="input-xlarge focused" id="focusedInput" name="focusedInput" type="text" value="This is focused…" />
  </div>
</div>';
my $rendered = $form->field('focusedInput')->render;
is_html( $rendered, $expected, 'focusedInput field renders ok' );

$expected =
'<div class="control-group">
  <label class="control-label" for="uneditableInput">Uneditable input</label>
  <div class="controls">
    <span id="uneditableInput" class="input-xlarge uneditable-input">Some value here</span>
  </div>
</div>';
$rendered = $form->field('uneditableInput')->render;
is_html( $rendered, $expected, 'uneditableInput field renders ok' );

$expected =
'<div class="control-group">
  <label class="control-label" for="disabledInput">Disabled input</label>
  <div class="controls">
    <input class="input-xlarge disabled" id="disabledInput" name="disabledInput" type="text" placeholder="Disabled input here…" disabled="disabled" value="" />
  </div>
</div>';
$rendered = $form->field('disabledInput')->render;
is_html( $rendered, $expected, 'disabledInput field renders ok' );

$expected =
'<div class="control-group">
  <label class="control-label" for="optionsCheckbox2">Disabled checkbox</label>
  <div class="controls">
    <label class="checkbox" for="optionsCheckbox2">
      <input class="checkbox disabled" type="checkbox" id="optionsCheckbox2" name="optionsCheckbox2" value="option1" disabled="disabled" />
      This is a disabled checkbox
    </label>
  </div>
</div>';
$rendered = $form->field('optionsCheckbox2')->render;
is_html( $rendered, $expected, 'optionsCheckbox2 renders ok');

$expected =
'<div class="control-group warning">
  <label class="control-label" for="inputError1">Input with warning</label>
  <div class="controls">
    <input class="warning" type="text" id="inputError1" name="inputError1" value="entry" />
    <span class="help-inline">Something may have gone wrong</span>
  </div>
</div>';
$rendered = $form->field('inputError1')->render;
is_html( $rendered, $expected, 'inputError1 renders ok' );

$expected =
'<div class="control-group error">
  <label class="control-label" for="inputError2">Input with error</label>
  <div class="controls">
    <input class="error" type="text" id="inputError2" name="inputError2" value="xxx" />
    <span class="help-inline">Please correct the error</span>
  </div>
</div>';
$rendered = $form->field('inputError2')->render;
is_html( $rendered, $expected, 'inputError2 renders ok' );

# this doesn't come from actual processing; could add something to set
$expected =
'<div class="control-group success">
  <label class="control-label" for="inputError3">Input with success</label>
  <div class="controls">
    <input type="text" id="inputError3" name="inputError3" value="success" />
    <span class="help-inline">Woohoo!</span>
  </div>
</div>';
$rendered = $form->field('inputError3')->render;
is_html( $rendered, $expected, 'inputError3 renders ok' );

$expected =
'<div class="control-group success">
  <label class="control-label" for="selectError">Select with success</label>
  <div class="controls">
    <select id="selectError" name="selectError">
      <option value="1" id="selectError.0">1</option>
      <option value="2" id="selectError.1">2</option>
      <option value="3" id="selectError.2">3</option>
      <option value="4" id="selectError.3">4</option>
      <option value="5" id="selectError.4">5</option>
    </select>
    <span class="help-inline">Woohoo!</span>
  </div>
</div>';
$rendered = $form->field('selectError')->render;
is_html( $rendered, $expected, 'selectError rendered ok' );

$expected =
'<div class="form-actions" id="form_actions">
  <button type="submit" class="btn btn-primary" name="form_actions.save" id="form_actions.save">Save changes</button>
  <button type="reset" class="btn" name="form_actions.cancel" id="form_actions.cancel">Cancel</button>
</div>';

$rendered = $form->field('form_actions')->render;
is_html( $rendered, $expected, 'form_actions rendered ok' );

$expected =
'<form id="test_form" class="form-horizontal" method="post">
<fieldset>
  <legend>Form control states</legend>
  <div class="alert alert-error"><span class="error_message">There were errors in your form</span></div>
  <div class="control-group">
    <label class="control-label" for="focusedInput">Focused input</label>
    <div class="controls">
      <input class="input-xlarge focused" id="focusedInput" name="focusedInput" type="text" value="This is focused…" />
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="uneditableInput">Uneditable input</label>
    <div class="controls">
      <span id="uneditableInput" class="input-xlarge uneditable-input">Some value here</span>
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="disabledInput">Disabled input</label>
    <div class="controls">
      <input class="input-xlarge disabled" id="disabledInput" name="disabledInput" type="text" placeholder="Disabled input here…" disabled="disabled" value="" />
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="optionsCheckbox2">Disabled checkbox</label>
    <div class="controls">
      <label class="checkbox" for="optionsCheckbox2">
        <input class="checkbox disabled" type="checkbox" id="optionsCheckbox2" name="optionsCheckbox2" value="option1" disabled="disabled" />
        This is a disabled checkbox
      </label>
    </div>
  </div>
  <div class="control-group warning">
    <label class="control-label" for="inputError1">Input with warning</label>
    <div class="controls">
      <input class="warning" type="text" id="inputError1" name="inputError1" value="entry" />
      <span class="help-inline">Something may have gone wrong</span>
    </div>
  </div>
  <div class="control-group error">
    <label class="control-label" for="inputError2">Input with error</label>
    <div class="controls">
      <input class="error" type="text" id="inputError2" name="inputError2" value="xxx" />
      <span class="help-inline">Please correct the error</span>
    </div>
  </div>
  <div class="control-group success">
    <label class="control-label" for="inputError3">Input with success</label>
    <div class="controls">
      <input type="text" id="inputError3" name="inputError3" value="success" />
      <span class="help-inline">Woohoo!</span>
    </div>
  </div>
  <div class="control-group success">
    <label class="control-label" for="selectError">Select with success</label>
    <div class="controls">
      <select id="selectError" name="selectError">
        <option value="1" id="selectError.0">1</option>
        <option value="2" id="selectError.1">2</option>
        <option value="3" id="selectError.2">3</option>
        <option value="4" id="selectError.3">4</option>
        <option value="5" id="selectError.4">5</option>
      </select>
      <span class="help-inline">Woohoo!</span>
    </div>
  </div>
  <div class="form-actions" id="form_actions">
    <button type="submit" class="btn btn-primary" name="form_actions.save" id="form_actions.save">Save changes</button>
    <button type="reset" class="btn" name="form_actions.cancel" id="form_actions.cancel">Cancel</button>
  </div>
</fieldset>
</form>';

$rendered = $form->render;
is_html( $rendered, $expected, 'form rendered ok' );

done_testing;