| 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 'opt_in' => ( |
| 12 |
type => 'Select', |
| 13 |
widget => 'RadioGroup', |
| 14 |
options => [ |
| 15 |
{ value => 'no & never', label => 'No & Never', }, |
| 16 |
{ value => '"yes"', label => 'Yes' }, |
| 17 |
] |
| 18 |
); |
| 19 |
|
| 20 |
has_field 'option_group' => ( |
| 21 |
type => 'Select', |
| 22 |
widget => 'RadioGroup', |
| 23 |
options => [ |
| 24 |
{ |
| 25 |
group => 'First Group', |
| 26 |
options => [ { value => 1, label => 'Yes'}, {value => 2, label => 'No' } ], |
| 27 |
attributes => { class => 'firstgroup' }, |
| 28 |
label_attributes => { class => 'group1' }, |
| 29 |
} |
| 30 |
] |
| 31 |
) |
| 32 |
} |
| 33 |
|
| 34 |
my $form = Test::Form->new; |
| 35 |
$form->process; |
| 36 |
my $expected = |
| 37 |
'<div> |
| 38 |
<label for="opt_in">Opt in</label> |
| 39 |
<label class="radio" for="opt_in.0"> |
| 40 |
<input type="radio" value="no & never" name="opt_in" id="opt_in.0" /> |
| 41 |
No & Never |
| 42 |
</label> |
| 43 |
<label class="radio" for="opt_in.1"> |
| 44 |
<input type="radio" value=""yes"" name="opt_in" id="opt_in.1" /> |
| 45 |
Yes |
| 46 |
</label> |
| 47 |
</div>'; |
| 48 |
|
| 49 |
my $rendered = $form->field('opt_in')->render; |
| 50 |
is_html( $rendered, $expected, 'radio group rendered ok' ); |
| 51 |
|
| 52 |
my $params = { |
| 53 |
opt_in => 'no & never', |
| 54 |
}; |
| 55 |
$form->process( update_field_list => { opt_in => { tags => { 'radio_br_after' => 1 }}}, params => $params); |
| 56 |
$rendered = $form->field('opt_in')->render; |
| 57 |
$expected = |
| 58 |
'<div> |
| 59 |
<label for="opt_in">Opt in</label><br /> |
| 60 |
<label class="radio" for="opt_in.0"> |
| 61 |
<input type="radio" value="no & never" name="opt_in" id="opt_in.0" checked="checked" /> |
| 62 |
No & Never |
| 63 |
</label><br /> |
| 64 |
<label class="radio" for="opt_in.1"><input type="radio" value=""yes"" name="opt_in" id="opt_in.1" /> |
| 65 |
Yes |
| 66 |
</label><br /> |
| 67 |
</div>'; |
| 68 |
|
| 69 |
is_html( $rendered, $expected, 'output from radio group'); |
| 70 |
|
| 71 |
# option group attributes |
| 72 |
$rendered = $form->field('option_group')->render; |
| 73 |
$expected = |
| 74 |
'<div> |
| 75 |
<label for="option_group">Option group</label> |
| 76 |
<div class="firstgroup"> |
| 77 |
<label class="group1">First Group</label> |
| 78 |
<label class="radio" for="option_group.0"> |
| 79 |
<input type="radio" value="1" name="option_group" id="option_group.0" /> |
| 80 |
Yes |
| 81 |
</label> |
| 82 |
<label class="radio" for="option_group.1"> |
| 83 |
<input type="radio" value="2" name="option_group" id="option_group.1" /> |
| 84 |
No |
| 85 |
</label> |
| 86 |
</div> |
| 87 |
</div>'; |
| 88 |
|
| 89 |
is_html( $rendered, $expected, 'output from ragio group with option group and label attributes'); |
| 90 |
|
| 91 |
# create form with no label rendering for opt_in |
| 92 |
$form = Test::Form->new( field_list => [ '+opt_in' => { do_label => 0 } ] ); |
| 93 |
$form->process; |
| 94 |
# first individually rendered option |
| 95 |
$rendered = $form->field('opt_in')->render_option({ value => 'test', label => 'Test'}); |
| 96 |
$expected = '<label class="radio" for="opt_in.0"><input id="opt_in.0" name="opt_in" type="radio" value="test" /> Test </label>'; |
| 97 |
is_html( $rendered, $expected, 'individual option rendered ok' ); |
| 98 |
# second rendered option is wrapped |
| 99 |
$rendered = $form->field('opt_in')->render_wrapped_option({ value => 'abcde', label => 'Abcde' }); |
| 100 |
$expected = '<div><label class="radio" for="opt_in.1"><input id="opt_in.1" name="opt_in" type="radio" value="abcde" /> Abcde </label></div>'; |
| 101 |
is_html( $rendered, $expected, 'indvidual wrapped option rendered ok' ); |
| 102 |
|
| 103 |
done_testing; |