| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use HTML::FormHandler::Test; |
| 5 |
|
| 6 |
{ |
| 7 |
package Test::Form; |
| 8 |
use HTML::FormHandler::Moose; |
| 9 |
extends 'HTML::FormHandler'; |
| 10 |
|
| 11 |
has_field 'comedians' => ( |
| 12 |
type => 'Multiple', |
| 13 |
widget => 'CheckboxGroup', |
| 14 |
options => [ |
| 15 |
{ value => 'keaton', label => 'Buster Keaton'}, |
| 16 |
{ value => 'chaplin', label => 'Charly Chaplin'}, |
| 17 |
{ value => 'laurel & hardy', label => 'Stan Laurel & Oliver Hardy' }, |
| 18 |
], |
| 19 |
); |
| 20 |
has_field 'fruit' => ( |
| 21 |
type => 'Multiple', |
| 22 |
widget => 'CheckboxGroup', |
| 23 |
tags => { inline => 1 }, |
| 24 |
options => [ |
| 25 |
{ value => 1, label => 'Apples' }, |
| 26 |
{ value => 2, label => 'Oranges' }, |
| 27 |
{ value => 3, label => 'Pears' }, |
| 28 |
], |
| 29 |
); |
| 30 |
|
| 31 |
has_field 'optgroup' => ( |
| 32 |
type => 'Multiple', |
| 33 |
widget => 'CheckboxGroup', |
| 34 |
); |
| 35 |
|
| 36 |
sub options_optgroup { ( |
| 37 |
{ |
| 38 |
group => 'First Group', |
| 39 |
options => [ |
| 40 |
{ value => 1, label => 'One' }, |
| 41 |
{ value => 2, label => 'Two' }, |
| 42 |
{ value => 3, label => 'Three' }, |
| 43 |
], |
| 44 |
attributes => { class => 'group1' }, |
| 45 |
}, |
| 46 |
{ |
| 47 |
group => 'Second Group', |
| 48 |
options => [ |
| 49 |
{ value => 4, label => 'Four' }, |
| 50 |
{ value => 5, label => 'Five' }, |
| 51 |
{ value => 6, label => 'Six' }, |
| 52 |
], |
| 53 |
label_attributes => { class => 'group2' }, |
| 54 |
}, |
| 55 |
) } |
| 56 |
|
| 57 |
} |
| 58 |
my $form = Test::Form->new; |
| 59 |
$form->process; |
| 60 |
|
| 61 |
my $expected = ' |
| 62 |
<div> |
| 63 |
<label for="comedians">Comedians</label> |
| 64 |
<label class="checkbox" for="comedians.0"> |
| 65 |
<input type="checkbox" value="keaton" name="comedians" id="comedians.0" /> |
| 66 |
Buster Keaton |
| 67 |
</label> |
| 68 |
<label class="checkbox" for="comedians.1"> |
| 69 |
<input type="checkbox" value="chaplin" name="comedians" id="comedians.1" /> |
| 70 |
Charly Chaplin |
| 71 |
</label> |
| 72 |
<label class="checkbox" for="comedians.2"> |
| 73 |
<input type="checkbox" value="laurel & hardy" name="comedians" id="comedians.2" /> |
| 74 |
Stan Laurel & Oliver Hardy |
| 75 |
</label> |
| 76 |
</div>'; |
| 77 |
my $rendered = $form->field('comedians')->render; |
| 78 |
is_html( $rendered, $expected, 'output from checkbox group'); |
| 79 |
|
| 80 |
$expected = |
| 81 |
'<div> |
| 82 |
<label for="fruit">Fruit</label> |
| 83 |
<label class="checkbox inline" for="fruit.0"> |
| 84 |
<input type="checkbox" value="1" name="fruit" id="fruit.0" /> |
| 85 |
Apples |
| 86 |
</label> |
| 87 |
<label class="checkbox inline" for="fruit.1"> |
| 88 |
<input type="checkbox" value="2" name="fruit" id="fruit.1" /> |
| 89 |
Oranges |
| 90 |
</label> |
| 91 |
<label class="checkbox inline" for="fruit.2"> |
| 92 |
<input type="checkbox" value="3" name="fruit" id="fruit.2" /> |
| 93 |
Pears |
| 94 |
</label> |
| 95 |
</div>'; |
| 96 |
$rendered = $form->field('fruit')->render; |
| 97 |
is_html( $rendered, $expected, 'output from inline checkbox group' ); |
| 98 |
|
| 99 |
my $params = { |
| 100 |
comedians => [ 'chaplin', 'laurel & hardy' ], |
| 101 |
}; |
| 102 |
$form->process($params); |
| 103 |
$rendered = $form->field('comedians')->render; |
| 104 |
$expected = |
| 105 |
'<div> |
| 106 |
<label for="comedians">Comedians</label> |
| 107 |
<label class="checkbox" for="comedians.0"> |
| 108 |
<input type="checkbox" value="keaton" name="comedians" id="comedians.0" /> |
| 109 |
Buster Keaton |
| 110 |
</label> |
| 111 |
<label class="checkbox" for="comedians.1"> |
| 112 |
<input type="checkbox" value="chaplin" name="comedians" id="comedians.1" checked="checked" /> |
| 113 |
Charly Chaplin |
| 114 |
</label> |
| 115 |
<label class="checkbox" for="comedians.2"> |
| 116 |
<input type="checkbox" value="laurel & hardy" name="comedians" id="comedians.2" checked="checked" /> |
| 117 |
Stan Laurel & Oliver Hardy |
| 118 |
</label> |
| 119 |
</div>'; |
| 120 |
is_html( $rendered, $expected, 'output from checkbox group' ); |
| 121 |
|
| 122 |
$rendered = $form->field('optgroup')->render; |
| 123 |
$expected = |
| 124 |
'<div> |
| 125 |
<label for="optgroup">Optgroup</label> |
| 126 |
<div class="group1"> |
| 127 |
<label>First Group</label> |
| 128 |
<label class="checkbox" for="optgroup.0"> |
| 129 |
<input type="checkbox" value="1" name="optgroup" id="optgroup.0" /> |
| 130 |
One |
| 131 |
</label> |
| 132 |
<label class="checkbox" for="optgroup.1"> |
| 133 |
<input type="checkbox" value="2" name="optgroup" id="optgroup.1" /> |
| 134 |
Two |
| 135 |
</label> |
| 136 |
<label class="checkbox" for="optgroup.2"> |
| 137 |
<input type="checkbox" value="3" name="optgroup" id="optgroup.2" /> |
| 138 |
Three |
| 139 |
</label> |
| 140 |
</div> |
| 141 |
<div> |
| 142 |
<label class="group2">Second Group</label> |
| 143 |
<label class="checkbox" for="optgroup.3"> |
| 144 |
<input type="checkbox" value="4" name="optgroup" id="optgroup.3" /> |
| 145 |
Four |
| 146 |
</label> |
| 147 |
<label class="checkbox" for="optgroup.4"> |
| 148 |
<input type="checkbox" value="5" name="optgroup" id="optgroup.4" /> |
| 149 |
Five |
| 150 |
</label> |
| 151 |
<label class="checkbox" for="optgroup.5"> |
| 152 |
<input type="checkbox" value="6" name="optgroup" id="optgroup.5" /> |
| 153 |
Six |
| 154 |
</label> |
| 155 |
</div> |
| 156 |
</div>'; |
| 157 |
|
| 158 |
is_html( $rendered, $expected, |
| 159 |
'output from checkbox group with option group attributes' ); |
| 160 |
|
| 161 |
done_testing; |