lib/HTML/FormFu/Element/Checkboxgroup.pm

filter: Statementstmt 97.1% 102 / 105 Branchbran 86.1% 31 / 36 Conditioncond 83.3% 25 / 30 MC/DCmcdc 66.6% 10 / 15 Subroutinesub 92.8% 13 / 14 Podpod n/a totaltotal 90.5% 181 / 200 SCAR 35.4
CC
34
Cov
92%
_prepare_attrs
14.2
_prepare_id
7.2
_string_field
7.0
CRAP 34.5
« lib/HTML/FormFu/Role/Element/Input.pm ↑ Summary lib/HTML/FormFu/Element/Block.pm »
1 package HTML::FormFu::Element::Checkboxgroup;
2  
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 our $VERSION = '2.05'; # VERSION
5  
6 20 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use MooseX::Attribute::FormFuChained;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 extends 'HTML::FormFu::Element';
9  
10 20 with 'HTML::FormFu::Role::Element::Group';
11  
12 20 ▶use HTML::FormFu::Constants qw( $EMPTY_STR );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 20 ▶use HTML::FormFu::Util qw( append_xml_attribute process_attrs );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 20 ▶use List::Util 1.33 qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 20 has input_type => (
17 is => 'rw',
18 default => 'checkbox',
19 lazy => 1,
20 traits => ['FormFuChained'],
21 );
22  
23 20 has reverse_group => (
24 is => 'rw',
25 traits => ['FormFuChained'],
26 );
27  
28 after BUILD => sub {
29 65 ▶ my ( $self, $args ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
30  
31 65 $self->layout_field_filename('field_layout_checkboxgroup_field');
32 65 $self->label_tag('legend');
33 65 $self->container_tag('fieldset');
34 65 $self->multi_value(1);
35 65 $self->reverse_group(1);
36 65 $self->input_type('checkbox');
37  
38 65 $self->layout( [
39 'label',
40 'errors',
41 'field',
42 'comment',
43 'javascript',
44 ] );
45  
46 65 return;
47 20 };
48  
49 sub prepare_id {
50 48 ▶ my ( $self, $render ) = @_;
Subroutine prepare_id: called CC=6 SCAR=17.9
51  
52 48 ▶ my $form_id = defined $self->form->id ? $self->form->id : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->form->id ? : 3 45 48
53 48 ▶ my $field_name = defined $self->nested_name ? $self->nested_name : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->nested_name ? : 48 0 48
54 48 my $count = 0;
55  
56 48 for my $option ( @{ $render->{options} } ) {
57 122 ▶ if ( exists $option->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{'group'}) { } 15 107 122
58 15 for my $item ( @{ $option->{group} } ) {
59 30 $self->_prepare_id( $item, $form_id, $field_name, \$count );
60 }
61 }
62 else {
63 107 $self->_prepare_id( $option, $form_id, $field_name, \$count );
64 }
65 }
66  
67 48 return;
68 }
69  
70 sub _prepare_id {
71 137 ▶ my ( $self, $option, $form_id, $field_name, $count_ref ) = @_;
Subroutine _prepare_id: called CC=7 SCAR=19.8
72  
73 137 ▶ if ( !exists $option->{attributes}{id} && defined $self->auto_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $option->{"attributes"}{"id"} and defined $self->auto_id) 6 131 137
Condition66%2/3
not exists $option->{"attributes"}{"id"} and defined $self->auto_id
!ll&&!rl&&r
01316
MC/DC50%1/2
not exists $option->{"attributes"}{"id"} and defined $self->auto_id
not exists $option->{"attributes"}{"id"} defined $self->auto_id
Truth table66%2/3
not(A) and B
A= not exists $option->{"attributes"}{"id"}B= defined $self->auto_id
ABresult
0X0
100
111
74 6 my %string = (
75 f => $form_id,
76 n => $field_name,
77 );
78  
79 6 my $id = $self->auto_id;
80 6 $id =~ s/%([fn])/$string{$1}/g;
81 6 $id =~ s/%c/ ++$$count_ref /gex;
82 6 $id =~ s/%v/ $option->{value} /gex;
83  
84 6 ▶ if ( defined( my $count = $self->repeatable_count ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $count = $self->repeatable_count)) 0 6 6
85 0 $id =~ s/%r/$count/g;
86 }
87  
88 6 $option->{attributes}{id} = $id;
89 }
90  
91 # label "for" attribute
92 137 ▶ if ( exists $option->{label}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option->{"label"} and exists $option->{"attributes"}{"id"} and not exists $option->{"label_attributes"}{"for"}) 6 131 137
Condition66%4/6
exists $option->{"label"} and exists $option->{"attributes"}{"id"} and not exists $option->{"label_attributes"}{"for"}
!ll&&!rl&&r
13106
exists $option->{"label"} and exists $option->{"attributes"}{"id"}
!ll&&!rl&&r
01316
MC/DC33%1/3
exists $option->{"label"} and exists $option->{"attributes"}{"id"} and not exists $option->{"label_attributes"}{"for"}
exists $option->{"label"} exists $option->{"attributes"}{"id"} not exists $option->{"label_attributes"}{"for"}
Truth table50%2/4
A and B and not(C)
A= exists $option->{"label"}B= exists $option->{"attributes"}{"id"}C= not exists $option->{"label_attributes"}{"for"}
ABCresult
0XX0
10X0
1100
1111
93 && exists $option->{attributes}{id}
94 && !exists $option->{label_attributes}{for} )
95 {
96 6 $option->{label_attributes}{for} = $option->{attributes}{id};
97 }
98  
99 137 return;
100 }
101  
102 sub _prepare_attrs {
103 137 ▶ my ( $self, $submitted, $value, $default, $option ) = @_;
Subroutine _prepare_attrs: called CC=14 SCAR=26.5
104  
105 137 ▶ if ( $submitted
Branch: 5 branches
BranchTrueFalseTotal
if ($submitted and defined $value and ref $value eq 'ARRAY' ? &any(sub { $_ eq $option->{'value'}; } , @$value) : $value eq $option->{'value'}) { } 15 122 137
elsif ($submitted and $self->retain_default and !defined($value) || $value eq $HTML::FormFu::Element::Checkboxgroup::EMPTY_STR and $self->value eq $option->{'value'}) { } 4 118 122
elsif ($submitted) { } 52 66 118
elsif (defined $default and ref $default eq 'ARRAY' ? &any(sub { $_ eq $option->{'value'}; } , @$default) : $default eq $option->{'value'}) { } 8 101 109
ref $value eq 'ARRAY' ? : 2 61 63
Condition100%6/6
$submitted and defined $value and ref $value eq "ARRAY" ? &any(sub {
!ll&&!rl&&r
744815
$submitted and defined $value
!ll&&!rl&&r
66863
MC/DC100%3/3
$submitted and defined $value and ref $value eq "ARRAY" ? &any(sub { $_ eq $option->{"value"}; } , @$value) : $value eq $option->{"value"}
$submitted defined $value ref $value eq "ARRAY" ? &any(sub { $_ eq $option->{"value"}; } , @$value) : $value eq $option->{"value"}
Truth table100%4/4
A and B and C
A= $submittedB= defined $valueC= ref $value eq "ARRAY" ? &any(sub { $_ eq $option->{"value"}; } , @$value) : $value eq $option->{"value"}
ABCresult
0XX0
10X0
1100
1111
106 && defined $value
107 && (ref $value eq 'ARRAY'
108 3 ▶ ? any { $_ eq $option->{value} } @$value
Subroutine __ANON__: called CC=1 SCAR=0.0
109 : $value eq $option->{value} ) )
110 {
111 15 $option->{attributes}{checked} = 'checked';
112 }
113 ▶ elsif ($submitted
Condition83%10/12
$submitted and $self->retain_default and !defined($value) || $value eq $HTML::FormFu::Element::Checkboxgroup::EMPTY_STR and $self->value eq $option->{"value"}
!ll&&!rl&&r
11084
$submitted and $self->retain_default and !defined($value) || $value eq $HTML::FormFu::Element::Checkboxgroup::EMPTY_STR
!ll&&!rl&&r
110012
$submitted and $self->retain_default
!ll&&!rl&&r
664412
not defined $value or $value eq $HTML::FormFu::Element::Checkboxgroup::EMPTY_STR
l!l&&r!l&&!r
660
MC/DC60%3/5
$submitted and $self->retain_default and !defined($value) || $value eq $HTML::FormFu::Element::Checkboxgroup::EMPTY_STR and $self->value eq $option->{"value"}
$submitted $self->retain_default not defined $value $value eq $HTML::FormFu::Element::Checkboxgroup::EMPTY_STR $self->value eq $option->{"value"}
Truth table85%6/7
A and B and not(C) or D and E
A= $submittedB= $self->retain_defaultC= not defined $valueD= $value eq $HTML::FormFu::Element::Checkboxgroup::EMPTY_STRE= $self->value eq $option->{"value"}
ABCDEresult
0XXXX0
10XXX0
1100X0
111X00
110100
111X11
110111
114 && $self->retain_default
115 && ( !defined $value || $value eq $EMPTY_STR )
116 && $self->value eq $option->{value} )
117 {
118 4 $option->{attributes}{checked} = 'checked';
119 }
120 elsif ($submitted) {
121 52 delete $option->{attributes}{checked};
122 }
123 ▶ elsif (
Branch: 1 branch
BranchTrueFalseTotal
ref $default eq 'ARRAY' ? : 0 23 23
Condition100%3/3
defined $default and ref $default eq "ARRAY" ? &any(sub {
!ll&&!rl&&r
43158
MC/DC100%2/2
defined $default and ref $default eq "ARRAY" ? &any(sub { $_ eq $option->{"value"}; } , @$default) : $default eq $option->{"value"}
defined $default ref $default eq "ARRAY" ? &any(sub { $_ eq $option->{"value"}; } , @$default) : $default eq $option->{"value"}
Truth table100%3/3
A and B
A= defined $defaultB= ref $default eq "ARRAY" ? &any(sub { $_ eq $option->{"value"}; } , @$default) : $default eq $option->{"value"}
ABresult
0X0
100
111
124 defined $default
125 && (ref $default eq 'ARRAY'
126 0 ▶ ? any { $_ eq $option->{value} } @$default
Subroutine __ANON__: not called CC=1 SCAR=6.9
127 : $default eq $option->{value} ) )
128 {
129 8 $option->{attributes}{checked} = 'checked';
130 }
131 137 return;
132 }
133  
134 sub render_data_non_recursive {
135 48 ▶ my ( $self, $args ) = @_;
Subroutine render_data_non_recursive: called CC=4 SCAR=13.9
136  
137 48 ▶ my $render = $self->SUPER::render_data_non_recursive( {
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 48 0 48
138 field_filename => $self->field_filename,
139 reverse_group => $self->reverse_group,
140 input_type => $self->input_type,
141 $args ? %$args : (),
142 } );
143  
144 48 for my $item ( @{ $render->{options} } ) {
145 122 ▶ if ( exists $item->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{"group"}) 15 107 122
146 15 append_xml_attribute( $item->{attributes}, 'class', 'subgroup' );
147 }
148 }
149  
150 48 return $render;
151 }
152  
153 sub _string_field {
154 48 ▶ my ( $self, $render ) = @_;
Subroutine _string_field: called CC=7 SCAR=19.5
155  
156 # radiogroup_tag template
157  
158 48 my $html .= sprintf "<span%s>\n", process_attrs( $render->{attributes} );
159  
160 48 for my $option ( @{ $render->{options} } ) {
161 122 ▶ if ( defined $option->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $option->{'group'}) { } 15 107 122
162 $html .= sprintf "<span%s>\n",
163 15 process_attrs( $option->{attributes} ),
164 ;
165  
166 15 ▶ if ( defined $option->{label} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $option->{"label"}) 8 7 15
167 8 $html .= sprintf "<p>%s</p>\n", $option->{label};
168 }
169  
170 15 for my $item ( @{ $option->{group} } ) {
171 $html .= sprintf
172 "<span%s>\n",
173 30 process_attrs( $item->{container_attributes} );
174  
175 my $label = sprintf
176 "<label%s>%s</label>\n",
177 process_attrs( $item->{label_attributes} ),
178 $item->{label},
179 30 ;
180  
181 my $input = sprintf
182 qq{<input name="%s" type="%s" value="%s"%s />\n},
183 $render->{nested_name},
184 $render->{input_type},
185 $item->{value},
186 30 process_attrs( $item->{attributes} ),
187 ;
188  
189 30 ▶ if ( $render->{reverse_group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($render->{'reverse_group'}) { } 30 0 30
190 30 $html .= $input . $label;
191 }
192 else {
193 0 $html .= $label . $input;
194 }
195  
196 30 $html .= "</span>\n";
197 }
198  
199 15 $html .= "</span>\n";
200 }
201 else {
202 $html .= sprintf
203 "<span%s>\n",
204 107 process_attrs( $option->{container_attributes} );
205  
206 my $label = sprintf
207 "<label%s>%s</label>\n",
208 process_attrs( $option->{label_attributes} ),
209 $option->{label},
210 107 ;
211  
212 my $input = sprintf
213 qq{<input name="%s" type="%s" value="%s"%s />\n},
214 $render->{nested_name},
215 $render->{input_type},
216 $option->{value},
217 107 process_attrs( $option->{attributes} ),
218 ;
219  
220 107 ▶ if ( $render->{reverse_group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($render->{'reverse_group'}) { } 99 8 107
221 99 $html .= $input . $label;
222 }
223 else {
224 8 $html .= $label . $input;
225 }
226  
227 107 $html .= "</span>\n";
228 }
229 }
230  
231 48 $html .= "</span>";
232  
233 48 return $html;
234 }
235  
236 20 __PACKAGE__->meta->make_immutable;
237  
238 20 1;
239  
240 __END__
241  
242 =head1 NAME
243  
244 HTML::FormFu::Element::Checkboxgroup - Group of checkbox form fields
245  
246 =head1 VERSION
247  
248 version 2.05
249  
250 =head1 SYNOPSIS
251  
252 YAML config:
253  
254 ---
255 elements:
256 - type: Checkboxgroup
257 name: subjects
258 options:
259 - [ 'Math' ]
260 - [ 'Science' ]
261 - [ 'English' ]
262  
263 =head1 DESCRIPTION
264  
265 Convenient to use group of checkbox fields.
266  
267 Use the same syntax as you would to create a Select element optgroup to
268 create Checkboxgroup sub-groups, see L<HTML::FormFu::Role::Element::Group/options>
269 for details.
270  
271 =head1 METHODS
272  
273 =head2 options
274  
275 See L<HTML::FormFu::Role::Element::Group/options>.
276  
277 =head2 values
278  
279 See L<HTML::FormFu::Role::Element::Group/values>.
280  
281 =head2 value_range
282  
283 See L<HTML::FormFu::Role::Element::Group/value_range>.
284  
285 =head2 auto_id
286  
287 In addition to the substitutions documented by L<HTML::FormFu/auto_id>,
288 C<%c> will be replaced by an incremented integer, to ensure there are
289 no duplicated ID's, and C<%v> will be replaced by the item's value to
290 allow multiple elements with the same name to coexist, and their labels
291 to correctly select the appropriate item.
292  
293 ---
294 elements:
295 type: Checkboxgroup
296 name: foo
297 auto_id: "%n_%c"
298  
299 =head2 reverse_group
300  
301 If true, then the label for each checkbox in the checkbox group should
302 be rendered to the right of the field control. Otherwise, the label
303 is rendered to the left of the field control.
304  
305 The default value is C<true>, causing each label to be rendered to the
306 right of its field control (or to be explicit: the markup for the
307 label comes after the field control in the source).
308  
309 Default Value: C<true>
310  
311 =head1 SEE ALSO
312  
313 Is a sub-class of, and inherits methods from
314 L<HTML::FormFu::Role::Element::Group>,
315 L<HTML::FormFu::Role::Element::Field>,
316 L<HTML::FormFu::Element>
317  
318 L<HTML::FormFu>
319  
320 =head1 AUTHOR
321  
322 Carl Franks, C<cfranks@cpan.org>
323  
324 =head1 LICENSE
325  
326 This library is free software, you can redistribute it and/or modify it under
327 the same terms as Perl itself.
328  
329 =cut
« lib/HTML/FormFu/Role/Element/Input.pm ↑ Summary lib/HTML/FormFu/Element/Block.pm »