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