Skip to content

Loading…

Bugfix/option group labels #35

Merged
merged 2 commits into from

2 participants

@veryrusty

Both the CheckboxGroup and RadioGroup widgets (look like they) should support label_attributes when option groups are used from select fields.

@gshank gshank merged commit 2f8e4c2 into gshank:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
View
2 lib/HTML/FormHandler/Widget/Field/CheckboxGroup.pm
@@ -40,7 +40,7 @@ sub render_element {
my $attr = $option->{attributes} || {};
my $attr_str = process_attrs($attr);
my $lattr = $option->{label_attributes} || {};
- my $lattr_str= process_attrs($attr);
+ my $lattr_str= process_attrs($lattr);
$output .= qq{\n<div$attr_str><label$lattr_str>$label</label>};
foreach my $group_opt ( @{ $option->{options} } ) {
$output .= $self->render_option( $group_opt, $result );
View
2 lib/HTML/FormHandler/Widget/Field/RadioGroup.pm
@@ -33,7 +33,7 @@ sub render_element {
my $attr = $option->{attributes} || {};
my $attr_str = process_attrs($attr);
my $lattr = $option->{label_attributes} || {};
- my $lattr_str= process_attrs($attr);
+ my $lattr_str= process_attrs($lattr);
$output .= qq{\n<div$attr_str><label$lattr_str>$label</label>};
foreach my $group_opt ( @{ $option->{options} } ) {
$output .= $self->render_option( $group_opt, $result );
View
65 t/render/checkbox_group.t
@@ -28,6 +28,32 @@ use HTML::FormHandler::Test;
],
);
+ has_field 'optgroup' => (
+ type => 'Multiple',
+ widget => 'CheckboxGroup',
+ );
+
+ sub options_optgroup { (
+ {
+ group => 'First Group',
+ options => [
+ { value => 1, label => 'One' },
+ { value => 2, label => 'Two' },
+ { value => 3, label => 'Three' },
+ ],
+ attributes => { class => 'group1' },
+ },
+ {
+ group => 'Second Group',
+ options => [
+ { value => 4, label => 'Four' },
+ { value => 5, label => 'Five' },
+ { value => 6, label => 'Six' },
+ ],
+ label_attributes => { class => 'group2' },
+ },
+ ) }
+
}
my $form = Test::Form->new;
$form->process;
@@ -93,4 +119,43 @@ $expected =
</div>';
is_html( $rendered, $expected, 'output from checkbox group' );
+$rendered = $form->field('optgroup')->render;
+$expected =
+'<div>
+ <label for="optgroup">Optgroup</label>
+ <div class="group1">
+ <label>First Group</label>
+ <label class="checkbox" for="optgroup.0">
+ <input type="checkbox" value="1" name="optgroup" id="optgroup.0" />
+ One
+ </label>
+ <label class="checkbox" for="optgroup.1">
+ <input type="checkbox" value="2" name="optgroup" id="optgroup.1" />
+ Two
+ </label>
+ <label class="checkbox" for="optgroup.2">
+ <input type="checkbox" value="3" name="optgroup" id="optgroup.2" />
+ Three
+ </label>
+ </div>
+ <div>
+ <label class="group2">Second Group</label>
+ <label class="checkbox" for="optgroup.3">
+ <input type="checkbox" value="4" name="optgroup" id="optgroup.3" />
+ Four
+ </label>
+ <label class="checkbox" for="optgroup.4">
+ <input type="checkbox" value="5" name="optgroup" id="optgroup.4" />
+ Five
+ </label>
+ <label class="checkbox" for="optgroup.5">
+ <input type="checkbox" value="6" name="optgroup" id="optgroup.5" />
+ Six
+ </label>
+ </div>
+</div>';
+
+is_html( $rendered, $expected,
+ 'output from checkbox group with option group attributes' );
+
done_testing;
View
33 t/render/radio_group.t
@@ -16,6 +16,19 @@ use HTML::FormHandler::Test;
{ value => '"yes"', label => 'Yes' },
]
);
+
+ has_field 'option_group' => (
+ type => 'Select',
+ widget => 'RadioGroup',
+ options => [
+ {
+ group => 'First Group',
+ options => [ { value => 1, label => 'Yes'}, {value => 2, label => 'No' } ],
+ attributes => { class => 'firstgroup' },
+ label_attributes => { class => 'group1' },
+ }
+ ]
+ )
}
my $form = Test::Form->new;
@@ -55,6 +68,26 @@ $expected =
is_html( $rendered, $expected, 'output from radio group');
+# option group attributes
+$rendered = $form->field('option_group')->render;
+$expected =
+'<div>
+ <label for="option_group">Option group</label>
+ <div class="firstgroup">
+ <label class="group1">First Group</label>
+ <label class="radio" for="option_group.0">
+ <input type="radio" value="1" name="option_group" id="option_group.0" />
+ Yes
+ </label>
+ <label class="radio" for="option_group.1">
+ <input type="radio" value="2" name="option_group" id="option_group.1" />
+ No
+ </label>
+ </div>
+</div>';
+
+is_html( $rendered, $expected, 'output from ragio group with option group and label attributes');
+
# create form with no label rendering for opt_in
$form = Test::Form->new( field_list => [ '+opt_in' => { do_label => 0 } ] );
$form->process;
Something went wrong with that request. Please try again.