| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use HTML::FormHandler::Test; |
| 5 |
|
| 6 |
# tests rendering an optgroup |
| 7 |
{ |
| 8 |
package MyApp::Form::Test; |
| 9 |
use HTML::FormHandler::Moose; |
| 10 |
extends 'HTML::FormHandler'; |
| 11 |
|
| 12 |
has_field 'foo'; |
| 13 |
has_field 'testop' => ( type => 'Select', multiple => 1, empty_select => '-- Choose --' ); |
| 14 |
sub options_testop { ( |
| 15 |
{ |
| 16 |
group => 'First Group', |
| 17 |
options => [ |
| 18 |
{ value => 1, label => 'One' }, |
| 19 |
{ value => 2, label => 'Two' }, |
| 20 |
{ value => 3, label => 'Three' }, |
| 21 |
], |
| 22 |
}, |
| 23 |
{ |
| 24 |
group => 'Second Group', |
| 25 |
options => [ |
| 26 |
{ value => 4, label => 'Four' }, |
| 27 |
{ value => 5, label => 'Five' }, |
| 28 |
{ value => 6, label => 'Six' }, |
| 29 |
], |
| 30 |
}, |
| 31 |
{ value => '6a', label => 'SixA' }, |
| 32 |
{ |
| 33 |
group => 'Third Group', |
| 34 |
options => [ |
| 35 |
{ value => 7, label => 'Seven' }, |
| 36 |
{ value => 8, label => 'Eight' }, |
| 37 |
{ value => 9, label => 'Nine' }, |
| 38 |
], |
| 39 |
}, |
| 40 |
|
| 41 |
) } |
| 42 |
} |
| 43 |
|
| 44 |
my $form = MyApp::Form::Test->new; |
| 45 |
ok( $form, 'form built' ); |
| 46 |
$form->process ( { foo => 'my_foo', testop => 12 } ); |
| 47 |
ok( ! $form->validated, 'form validated' ); |
| 48 |
my $params = { foo => 'my_foo', testop => 8 }; |
| 49 |
|
| 50 |
$form->process( $params ); |
| 51 |
ok( $form->validated, 'form validated' ); |
| 52 |
my $rendered = $form->field('testop')->render; |
| 53 |
my $expected = |
| 54 |
'<div><label for="testop">Testop</label> |
| 55 |
<select name="testop" id="testop" multiple="multiple"> |
| 56 |
<option value="" id="testop.0">-- Choose --</option> |
| 57 |
<optgroup label="First Group"> |
| 58 |
<option value="1" id="testop.1">One</option> |
| 59 |
<option value="2" id="testop.2">Two</option> |
| 60 |
<option value="3" id="testop.3">Three</option> |
| 61 |
</optgroup> |
| 62 |
<optgroup label="Second Group"> |
| 63 |
<option value="4" id="testop.4">Four</option> |
| 64 |
<option value="5" id="testop.5">Five</option> |
| 65 |
<option value="6" id="testop.6">Six</option> |
| 66 |
</optgroup> |
| 67 |
<option value="6a" id="testop.7">SixA</option> |
| 68 |
<optgroup label="Third Group"> |
| 69 |
<option value="7" id="testop.8">Seven</option> |
| 70 |
<option value="8" id="testop.9" selected="selected">Eight</option> |
| 71 |
<option value="9" id="testop.10">Nine</option> |
| 72 |
</optgroup> |
| 73 |
</select></div>'; |
| 74 |
|
| 75 |
is_html( $rendered, $expected, 'select rendered ok' ); |
| 76 |
|
| 77 |
$form = MyApp::Form::Test->new( update_subfields => { 'testop' => { widget => 'CheckboxGroup' } } ); |
| 78 |
$form->process( $params ); |
| 79 |
$rendered = $form->field('testop')->render; |
| 80 |
$expected = |
| 81 |
'<div> |
| 82 |
<label for="testop">Testop</label> |
| 83 |
<div> |
| 84 |
<label>First Group</label> |
| 85 |
<label class="checkbox" for="testop.0"><input id="testop.0" name="testop" type="checkbox" value="1" /> One </label> |
| 86 |
<label class="checkbox" for="testop.1"><input id="testop.1" name="testop" type="checkbox" value="2" /> Two </label> |
| 87 |
<label class="checkbox" for="testop.2"><input id="testop.2" name="testop" type="checkbox" value="3" /> Three </label> |
| 88 |
</div> |
| 89 |
<div> |
| 90 |
<label>Second Group</label> |
| 91 |
<label class="checkbox" for="testop.3"><input id="testop.3" name="testop" type="checkbox" value="4" /> Four </label> |
| 92 |
<label class="checkbox" for="testop.4"><input id="testop.4" name="testop" type="checkbox" value="5" /> Five </label> |
| 93 |
<label class="checkbox" for="testop.5"><input id="testop.5" name="testop" type="checkbox" value="6" /> Six </label> |
| 94 |
</div> |
| 95 |
<label class="checkbox" for="testop.6"><input id="testop.6" name="testop" type="checkbox" value="6a" /> SixA </label> |
| 96 |
<div> |
| 97 |
<label>Third Group</label> |
| 98 |
<label class="checkbox" for="testop.7"><input id="testop.7" name="testop" type="checkbox" value="7" /> Seven </label> |
| 99 |
<label class="checkbox" for="testop.8"><input checked="checked" id="testop.8" name="testop" type="checkbox" value="8" /> Eight </label> |
| 100 |
<label class="checkbox" for="testop.9"><input id="testop.9" name="testop" type="checkbox" value="9" /> Nine </label> |
| 101 |
</div> |
| 102 |
</div>'; |
| 103 |
|
| 104 |
is_html( $rendered, $expected, 'checkboxgroup renders ok' ); |
| 105 |
|
| 106 |
$form = MyApp::Form::Test->new( update_subfields => { 'testop' => { widget => 'RadioGroup', multiple => 0 } } ); |
| 107 |
$form->process( $params ); |
| 108 |
$rendered = $form->field('testop')->render; |
| 109 |
$expected = |
| 110 |
'<div> |
| 111 |
<label for="testop">Testop</label> |
| 112 |
<div><label>First Group</label> |
| 113 |
<label class="radio" for="testop.0"><input id="testop.0" name="testop" type="radio" value="1" /> One </label> |
| 114 |
<label class="radio" for="testop.1"><input id="testop.1" name="testop" type="radio" value="2" /> Two </label> |
| 115 |
<label class="radio" for="testop.2"><input id="testop.2" name="testop" type="radio" value="3" /> Three </label> |
| 116 |
</div> |
| 117 |
<div> |
| 118 |
<label>Second Group</label> |
| 119 |
<label class="radio" for="testop.3"><input id="testop.3" name="testop" type="radio" value="4" /> Four </label> |
| 120 |
<label class="radio" for="testop.4"><input id="testop.4" name="testop" type="radio" value="5" /> Five </label> |
| 121 |
<label class="radio" for="testop.5"><input id="testop.5" name="testop" type="radio" value="6" /> Six </label> |
| 122 |
</div> |
| 123 |
<label class="radio" for="testop.6"><input id="testop.6" name="testop" type="radio" value="6a" /> SixA </label> |
| 124 |
<div> |
| 125 |
<label>Third Group</label> |
| 126 |
<label class="radio" for="testop.7"><input id="testop.7" name="testop" type="radio" value="7" /> Seven </label> |
| 127 |
<label class="radio" for="testop.8"><input checked="checked" id="testop.8" name="testop" type="radio" value="8" /> Eight </label> |
| 128 |
<label class="radio" for="testop.9"><input id="testop.9" name="testop" type="radio" value="9" /> Nine </label> |
| 129 |
</div> |
| 130 |
</div>'; |
| 131 |
is_html( $rendered, $expected, 'radiogroup renders ok' ); |
| 132 |
|
| 133 |
done_testing; |