Debian Codegraph @

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

raw
1 package HTML::FormHandler::Widget::Field::HorizCheckboxGroup;
2 # ABSTRACT: checkbox group field role
3 $HTML::FormHandler::Widget::Field::HorizCheckboxGroup::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 my $lattr_str = process_attrs( $lattr );
58
59 my $id = $self->id . '.' . $self->options_index;
60
61 # start wrapping div
62 my $output = qq{<div class="checkbox">};
63
64 $output .= qq{\n<label$lattr_str for="$id">};
65 my $value = $option->{value};
66 $output .= qq{\n<input type="checkbox"};
67 $output .= qq{ value="} . $self->html_filter($value) . '"';
68 $output .= qq{ name="} . $self->html_name . '"';
69 $output .= qq{ id="$id"};
70
71 # handle option attributes
72 my $attr = $option->{attributes} || {};
73 if( defined $option->{disabled} && $option->{disabled} ) {
74 $attr->{disabled} = 'disabled';
75 }
76 if ( defined $fif &&
77 ( ( $self->multiple && exists $fif_lookup{$value} ) ||
78 ( $fif eq $value ) ) ) {
79 $attr->{checked} = 'checked';
80 }
81 $output .= process_attrs($attr);
82 $output .= " />\n";
83
84 # handle label
85 my $label = $option->{label};
86 $label = $self->_localize($label) if $self->localize_labels;
87 $output .= encode_entities($label, '<>&');
88 $output .= "\n</label>";
89
90 # close wrapping div
91 $output .= "\n</div>";
92
93 $self->inc_options_index;
94 return $output;
95 }
96
97 1;
98
99 __END__
100
101 =pod
102
103 =encoding UTF-8
104
105 =head1 NAME
106
107 HTML::FormHandler::Widget::Field::HorizCheckboxGroup - checkbox group field role
108
109 =head1 VERSION
110
111 version 0.410002
112
113 =head1 SYNOPSIS
114
115 Checkbox group widget for rendering multiple selects.
116
117 Wraps each checkbox in a div.
118
119 =head1 AUTHOR
120
121 FormHandler Contributors - see HTML::FormHandler
122
123 =head1 COPYRIGHT AND LICENSE
124
125 This software is copyright (c) 2017 by Gerda Shank.
126
127 This is free software; you can redistribute it and/or modify it under
128 the same terms as the Perl 5 programming language system itself.
129
130 =cut