| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use HTML::FormHandler::Test; |
| 5 |
$ENV{LANGUAGE_HANDLE} = 'en_en'; |
| 6 |
|
| 7 |
{ |
| 8 |
package MyApp::Form::CtrlStates::Theme; |
| 9 |
use Moose::Role; |
| 10 |
with 'HTML::FormHandler::Widget::Theme::Bootstrap'; |
| 11 |
|
| 12 |
sub build_form_tags {{ |
| 13 |
after_start => '<fieldset><legend>Form control states</legend>', |
| 14 |
before_end => '</fieldset>', |
| 15 |
}} |
| 16 |
|
| 17 |
sub build_update_subfields {{ |
| 18 |
focusedInput => { element_class => ['input-xlarge', 'focused'] }, |
| 19 |
uneditableInput => { element_class => ['input-xlarge', 'uneditable-input'] }, |
| 20 |
disabledInput => { element_class => ['input-xlarge'], |
| 21 |
element_attr => { placeholder => 'Disabled input here…' } }, |
| 22 |
optionsCheckbox2 => { element_class => ['checkbox'], |
| 23 |
option_label => 'This is a disabled checkbox' }, |
| 24 |
inputError3 => { wrapper_class => ['success'], |
| 25 |
tags => { after_element => qq{\n<span class="help-inline">Woohoo!</span>} } }, |
| 26 |
selectError => { wrapper_class => ['success'], |
| 27 |
tags => { after_element => qq{\n<span class="help-inline">Woohoo!</span>} } }, |
| 28 |
form_actions => { do_wrapper => 1, do_label => 0 }, |
| 29 |
'form_actions.save' => { widget_wrapper => 'None', element_class => ['btn', 'btn-primary'] }, |
| 30 |
'form_actions.cancel' => { widget_wrapper => 'None', element_class => ['btn'] }, |
| 31 |
}} |
| 32 |
} |
| 33 |
|
| 34 |
{ |
| 35 |
package MyApp::Form::CtrlStates; |
| 36 |
use HTML::FormHandler::Moose; |
| 37 |
extends 'HTML::FormHandler'; |
| 38 |
with 'MyApp::Form::CtrlStates::Theme'; |
| 39 |
|
| 40 |
has '+name' => ( default => 'test_form' ); |
| 41 |
has_field 'focusedInput' => ( default => 'This is focused…', label => 'Focused input' ); |
| 42 |
has_field 'uneditableInput' => ( type => 'NonEditable', label => 'Uneditable input', |
| 43 |
value => 'Some value here', |
| 44 |
); |
| 45 |
has_field 'disabledInput' => ( disabled => 1, label => 'Disabled input' ); |
| 46 |
has_field 'optionsCheckbox2' => ( type => 'Checkbox', checkbox_value => "option1", |
| 47 |
disabled => 1, label => 'Disabled checkbox' ); |
| 48 |
has_field 'inputError1' => ( validate_method => \&validate_ie1, |
| 49 |
label => 'Input with warning' ); |
| 50 |
has_field 'inputError2' => ( validate_method => \&validate_ie2, |
| 51 |
label => 'Input with error' );; |
| 52 |
has_field 'inputError3' => ( label => 'Input with success' );; |
| 53 |
has_field 'selectError' => ( type => 'Select', label => 'Select with success', |
| 54 |
options => [{ value => 1, label => '1' }, { value => 2, label => '2' }, |
| 55 |
{ value => 3, label => '3'}, |
| 56 |
{ value => 4, label => '4' }, { value => 5, label => '5' } ], |
| 57 |
); |
| 58 |
has_field 'form_actions' => ( type => 'Compound' ); |
| 59 |
has_field 'form_actions.save' => ( widget => 'ButtonTag', type => 'Submit', |
| 60 |
value => 'Save changes' ); |
| 61 |
has_field 'form_actions.cancel' => ( widget => 'ButtonTag', type => 'Reset', |
| 62 |
value => 'Cancel' ); |
| 63 |
sub validate_ie1 { |
| 64 |
my $self = shift; # self is the field here |
| 65 |
$self->add_warning('Something may have gone wrong'); |
| 66 |
} |
| 67 |
sub validate_ie2 { |
| 68 |
my $self = shift; |
| 69 |
$self->add_error('Please correct the error'); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
my $form = MyApp::Form::CtrlStates->new; |
| 74 |
|
| 75 |
my $params = $form->fif; |
| 76 |
$params->{inputError1} = 'entry'; |
| 77 |
$params->{inputError2} = 'xxx'; |
| 78 |
$params->{inputError3} = 'success'; |
| 79 |
$form->process( $params ); |
| 80 |
|
| 81 |
my $expected = |
| 82 |
'<div class="control-group"> |
| 83 |
<label class="control-label" for="focusedInput">Focused input</label> |
| 84 |
<div class="controls"> |
| 85 |
<input class="input-xlarge focused" id="focusedInput" name="focusedInput" type="text" value="This is focused…" /> |
| 86 |
</div> |
| 87 |
</div>'; |
| 88 |
my $rendered = $form->field('focusedInput')->render; |
| 89 |
is_html( $rendered, $expected, 'focusedInput field renders ok' ); |
| 90 |
|
| 91 |
$expected = |
| 92 |
'<div class="control-group"> |
| 93 |
<label class="control-label" for="uneditableInput">Uneditable input</label> |
| 94 |
<div class="controls"> |
| 95 |
<span id="uneditableInput" class="input-xlarge uneditable-input">Some value here</span> |
| 96 |
</div> |
| 97 |
</div>'; |
| 98 |
$rendered = $form->field('uneditableInput')->render; |
| 99 |
is_html( $rendered, $expected, 'uneditableInput field renders ok' ); |
| 100 |
|
| 101 |
$expected = |
| 102 |
'<div class="control-group"> |
| 103 |
<label class="control-label" for="disabledInput">Disabled input</label> |
| 104 |
<div class="controls"> |
| 105 |
<input class="input-xlarge disabled" id="disabledInput" name="disabledInput" type="text" placeholder="Disabled input here…" disabled="disabled" value="" /> |
| 106 |
</div> |
| 107 |
</div>'; |
| 108 |
$rendered = $form->field('disabledInput')->render; |
| 109 |
is_html( $rendered, $expected, 'disabledInput field renders ok' ); |
| 110 |
|
| 111 |
$expected = |
| 112 |
'<div class="control-group"> |
| 113 |
<label class="control-label" for="optionsCheckbox2">Disabled checkbox</label> |
| 114 |
<div class="controls"> |
| 115 |
<label class="checkbox" for="optionsCheckbox2"> |
| 116 |
<input class="checkbox disabled" type="checkbox" id="optionsCheckbox2" name="optionsCheckbox2" value="option1" disabled="disabled" /> |
| 117 |
This is a disabled checkbox |
| 118 |
</label> |
| 119 |
</div> |
| 120 |
</div>'; |
| 121 |
$rendered = $form->field('optionsCheckbox2')->render; |
| 122 |
is_html( $rendered, $expected, 'optionsCheckbox2 renders ok'); |
| 123 |
|
| 124 |
$expected = |
| 125 |
'<div class="control-group warning"> |
| 126 |
<label class="control-label" for="inputError1">Input with warning</label> |
| 127 |
<div class="controls"> |
| 128 |
<input class="warning" type="text" id="inputError1" name="inputError1" value="entry" /> |
| 129 |
<span class="help-inline">Something may have gone wrong</span> |
| 130 |
</div> |
| 131 |
</div>'; |
| 132 |
$rendered = $form->field('inputError1')->render; |
| 133 |
is_html( $rendered, $expected, 'inputError1 renders ok' ); |
| 134 |
|
| 135 |
$expected = |
| 136 |
'<div class="control-group error"> |
| 137 |
<label class="control-label" for="inputError2">Input with error</label> |
| 138 |
<div class="controls"> |
| 139 |
<input class="error" type="text" id="inputError2" name="inputError2" value="xxx" /> |
| 140 |
<span class="help-inline">Please correct the error</span> |
| 141 |
</div> |
| 142 |
</div>'; |
| 143 |
$rendered = $form->field('inputError2')->render; |
| 144 |
is_html( $rendered, $expected, 'inputError2 renders ok' ); |
| 145 |
|
| 146 |
# this doesn't come from actual processing; could add something to set |
| 147 |
$expected = |
| 148 |
'<div class="control-group success"> |
| 149 |
<label class="control-label" for="inputError3">Input with success</label> |
| 150 |
<div class="controls"> |
| 151 |
<input type="text" id="inputError3" name="inputError3" value="success" /> |
| 152 |
<span class="help-inline">Woohoo!</span> |
| 153 |
</div> |
| 154 |
</div>'; |
| 155 |
$rendered = $form->field('inputError3')->render; |
| 156 |
is_html( $rendered, $expected, 'inputError3 renders ok' ); |
| 157 |
|
| 158 |
$expected = |
| 159 |
'<div class="control-group success"> |
| 160 |
<label class="control-label" for="selectError">Select with success</label> |
| 161 |
<div class="controls"> |
| 162 |
<select id="selectError" name="selectError"> |
| 163 |
<option value="1" id="selectError.0">1</option> |
| 164 |
<option value="2" id="selectError.1">2</option> |
| 165 |
<option value="3" id="selectError.2">3</option> |
| 166 |
<option value="4" id="selectError.3">4</option> |
| 167 |
<option value="5" id="selectError.4">5</option> |
| 168 |
</select> |
| 169 |
<span class="help-inline">Woohoo!</span> |
| 170 |
</div> |
| 171 |
</div>'; |
| 172 |
$rendered = $form->field('selectError')->render; |
| 173 |
is_html( $rendered, $expected, 'selectError rendered ok' ); |
| 174 |
|
| 175 |
$expected = |
| 176 |
'<div class="form-actions" id="form_actions"> |
| 177 |
<button type="submit" class="btn btn-primary" name="form_actions.save" id="form_actions.save">Save changes</button> |
| 178 |
<button type="reset" class="btn" name="form_actions.cancel" id="form_actions.cancel">Cancel</button> |
| 179 |
</div>'; |
| 180 |
|
| 181 |
$rendered = $form->field('form_actions')->render; |
| 182 |
is_html( $rendered, $expected, 'form_actions rendered ok' ); |
| 183 |
|
| 184 |
$expected = |
| 185 |
'<form id="test_form" class="form-horizontal" method="post"> |
| 186 |
<fieldset> |
| 187 |
<legend>Form control states</legend> |
| 188 |
<div class="alert alert-error"><span class="error_message">There were errors in your form</span></div> |
| 189 |
<div class="control-group"> |
| 190 |
<label class="control-label" for="focusedInput">Focused input</label> |
| 191 |
<div class="controls"> |
| 192 |
<input class="input-xlarge focused" id="focusedInput" name="focusedInput" type="text" value="This is focused…" /> |
| 193 |
</div> |
| 194 |
</div> |
| 195 |
<div class="control-group"> |
| 196 |
<label class="control-label" for="uneditableInput">Uneditable input</label> |
| 197 |
<div class="controls"> |
| 198 |
<span id="uneditableInput" class="input-xlarge uneditable-input">Some value here</span> |
| 199 |
</div> |
| 200 |
</div> |
| 201 |
<div class="control-group"> |
| 202 |
<label class="control-label" for="disabledInput">Disabled input</label> |
| 203 |
<div class="controls"> |
| 204 |
<input class="input-xlarge disabled" id="disabledInput" name="disabledInput" type="text" placeholder="Disabled input here…" disabled="disabled" value="" /> |
| 205 |
</div> |
| 206 |
</div> |
| 207 |
<div class="control-group"> |
| 208 |
<label class="control-label" for="optionsCheckbox2">Disabled checkbox</label> |
| 209 |
<div class="controls"> |
| 210 |
<label class="checkbox" for="optionsCheckbox2"> |
| 211 |
<input class="checkbox disabled" type="checkbox" id="optionsCheckbox2" name="optionsCheckbox2" value="option1" disabled="disabled" /> |
| 212 |
This is a disabled checkbox |
| 213 |
</label> |
| 214 |
</div> |
| 215 |
</div> |
| 216 |
<div class="control-group warning"> |
| 217 |
<label class="control-label" for="inputError1">Input with warning</label> |
| 218 |
<div class="controls"> |
| 219 |
<input class="warning" type="text" id="inputError1" name="inputError1" value="entry" /> |
| 220 |
<span class="help-inline">Something may have gone wrong</span> |
| 221 |
</div> |
| 222 |
</div> |
| 223 |
<div class="control-group error"> |
| 224 |
<label class="control-label" for="inputError2">Input with error</label> |
| 225 |
<div class="controls"> |
| 226 |
<input class="error" type="text" id="inputError2" name="inputError2" value="xxx" /> |
| 227 |
<span class="help-inline">Please correct the error</span> |
| 228 |
</div> |
| 229 |
</div> |
| 230 |
<div class="control-group success"> |
| 231 |
<label class="control-label" for="inputError3">Input with success</label> |
| 232 |
<div class="controls"> |
| 233 |
<input type="text" id="inputError3" name="inputError3" value="success" /> |
| 234 |
<span class="help-inline">Woohoo!</span> |
| 235 |
</div> |
| 236 |
</div> |
| 237 |
<div class="control-group success"> |
| 238 |
<label class="control-label" for="selectError">Select with success</label> |
| 239 |
<div class="controls"> |
| 240 |
<select id="selectError" name="selectError"> |
| 241 |
<option value="1" id="selectError.0">1</option> |
| 242 |
<option value="2" id="selectError.1">2</option> |
| 243 |
<option value="3" id="selectError.2">3</option> |
| 244 |
<option value="4" id="selectError.3">4</option> |
| 245 |
<option value="5" id="selectError.4">5</option> |
| 246 |
</select> |
| 247 |
<span class="help-inline">Woohoo!</span> |
| 248 |
</div> |
| 249 |
</div> |
| 250 |
<div class="form-actions" id="form_actions"> |
| 251 |
<button type="submit" class="btn btn-primary" name="form_actions.save" id="form_actions.save">Save changes</button> |
| 252 |
<button type="reset" class="btn" name="form_actions.cancel" id="form_actions.cancel">Cancel</button> |
| 253 |
</div> |
| 254 |
</fieldset> |
| 255 |
</form>'; |
| 256 |
|
| 257 |
$rendered = $form->render; |
| 258 |
is_html( $rendered, $expected, 'form rendered ok' ); |
| 259 |
|
| 260 |
done_testing; |