Debian Codegraph @

lib/HTML/FormHandler/Widget/Field/CheckboxGroup.pm

raw
1 package HTML::FormHandler::Widget::Field::CheckboxGroup;
2 # ABSTRACT: checkbox group field role
3 $HTML::FormHandler::Widget::Field::CheckboxGroup::VERSION = '0.410002';
4 use Moose::Role;
5 use namespace::autoclean;
6 use HTML::FormHandler::Render::Util ('process_attrs');
7 use HTML::Entities qw( encode_entities );
8
9
10 sub render {
11 my ( $self, $result ) = @_;
12 $result ||= $self->result;
13 die "No result for form field '" . $self->full_name . "'. Field may be inactive." unless $result;
14 my $output = $self->render_element( $result );
15 return $self->wrap_field( $result, $output );
16 }
17
18 sub render_element {
19 my ( $self, $result ) = @_;
20 $result ||= $self->result;
21
22
23 # loop through options
24 my $output = '';
25 foreach my $option ( @{ $self->{options} } ) {
26 if ( my $label = $option->{group} ) {
27 $label = $self->_localize( $label ) if $self->localize_labels;
28 my $attr = $option->{attributes} || {};
29 my $attr_str = process_attrs($attr);
30 my $lattr = $option->{label_attributes} || {};
31 my $lattr_str= process_attrs($lattr);
32 $output .= qq{\n<div$attr_str><label$lattr_str>} . encode_entities($label, '<>&') . qq{</label>};
33 foreach my $group_opt ( @{ $option->{options} } ) {
34 $output .= $self->render_option( $group_opt, $result );
35 }
36 $output .= qq{\n</div>};
37 }
38 else {
39 $output .= $self->render_option( $option, $result );
40 }
41 }
42 $self->reset_options_index;
43 return $output;
44 }
45
46 sub render_option {
47 my ( $self, $option, $result ) = @_;
48 $result ||= $self->result;
49
50 # get existing values
51 my $fif = $result->fif;
52 my %fif_lookup;
53 @fif_lookup{@$fif} = () if $self->multiple;
54
55 # create option label attributes
56 my $lattr = $option->{label_attributes} || {};
57 push @{ $lattr->{class} }, 'checkbox';
58 if ( $self->get_tag('inline') ) {
59 my $class = 'inline';
60 $class = 'checkbox-inline' if $self->has_flag('is_b3');
61 push @{ $lattr->{class} }, $class;
62 }
63 my $lattr_str = process_attrs( $lattr );
64 my $id = $self->id . '.' . $self->options_index;
65 my $output = qq{\n<label$lattr_str for="$id">};
66 my $value = $option->{value};
67 $output .= qq{\n<input type="checkbox"};
68 $output .= qq{ value="} . $self->html_filter($value) . '"';
69 $output .= qq{ name="} . $self->html_name . '"';
70 $output .= qq{ id="$id"};
71
72 # handle option attributes
73 my $attr = $option->{attributes} || {};
74 if( defined $option->{disabled} && $option->{disabled} ) {
75 $attr->{disabled} = 'disabled';
76 }
77 if ( defined $fif &&
78 ( ( $self->multiple && exists $fif_lookup{$value} ) ||
79 ( $fif eq $value ) ) ) {
80 $attr->{checked} = 'checked';
81 }
82 $output .= process_attrs($attr);
83 $output .= " />\n";
84
85 # handle label
86 my $label = $option->{label};
87 $label = $self->_localize($label) if $self->localize_labels;
88 $output .= encode_entities($label, '<>&');
89 $output .= "\n</label>";
90 $self->inc_options_index;
91
92 if ($self->get_tag('checkbox_element_wrapper')) {
93 $output = qq{<div class="checkbox">$output</div>};
94 }
95 return $output;
96 }
97
98 1;
99
100 __END__
101
102 =pod
103
104 =encoding UTF-8
105
106 =head1 NAME
107
108 HTML::FormHandler::Widget::Field::CheckboxGroup - checkbox group field role
109
110 =head1 VERSION
111
112 version 0.410002
113
114 =head1 SYNOPSIS
115
116 Checkbox group widget for rendering multiple selects.
117
118 Checkbox element label class is 'checkbox', plus 'inline'
119 if the 'inline' tag is set.
120
121 Options hashrefs must have the keys 'value', and 'label'.
122 They may have an 'attributes' hashref key. The 'checked'
123 attribute should not be set in the options hashref. It should
124 be set by supplying a default value or from params input.
125
126 See L<HTML::FormHandler::Field::Select> for documentation on
127 select fields and options.
128
129 =head1 AUTHOR
130
131 FormHandler Contributors - see HTML::FormHandler
132
133 =head1 COPYRIGHT AND LICENSE
134
135 This software is copyright (c) 2017 by Gerda Shank.
136
137 This is free software; you can redistribute it and/or modify it under
138 the same terms as the Perl 5 programming language system itself.
139
140 =cut