FormHandler/Widget/Field/CheckboxGroup.pm

filter: Statementstmt 98.5% 70 / 71 Branchbran 75.0% 15 / 20 Conditioncond 55.1% 16 / 29 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 3 totaltotal 75.1% 109 / 145 CC 24 SCAR 24.3
CC
24
Cov
84%
render_option
16.9
render_element
8.1
render
3.1
CRAP 11.4
« FormHandler/Model/Object.pm ↑ Summary FormHandler/Field/Select.pm »
1 package HTML::FormHandler::Widget::Field::CheckboxGroup;
2 # ABSTRACT: checkbox group field role
3 7 $HTML::FormHandler::Widget::Field::CheckboxGroup::VERSION = '0.410001';
4 7 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use HTML::FormHandler::Render::Util ('process_attrs');
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 sub render {
10 10 ▶ my ( $self, $result ) = @_;
Subroutine render: called CC=3 SCAR=11.2
Pod undocumented
11 10 ▶ $result ||= $self->result;
Condition66%2/3
$result ||= $self->result
l!l&&r!l&&!r
280
MC/DC0%0/2
$result ||= $self->result
$result $self->result
Truth table66%2/3
A ||= B
A= $resultB= $self->result
ABresult
1X1
011
000
12 10 ▶ die "No result for form field '" . $self->full_name . "'. Field may be inactive." unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 10 10
13 10 my $output = $self->render_element( $result );
14 10 return $self->wrap_field( $result, $output );
15 }
16  
17 sub render_element {
18 10 ▶ my ( $self, $result ) = @_;
Subroutine render_element: called CC=8 SCAR=20.9
Pod undocumented
19 10 ▶ $result ||= $self->result;
Condition33%1/3
$result ||= $self->result
l!l&&r!l&&!r
1000
MC/DC0%0/2
$result ||= $self->result
$result $self->result
Truth table33%1/3
A ||= B
A= $resultB= $self->result
ABresult
1X1
011
000
20  
21  
22 # loop through options
23 10 my $output = '';
24 10 foreach my $option ( @{ $self->{options} } ) {
25 30 ▶ if ( my $label = $option->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $label = $option->{'group'}) { } 5 25 30
26 5 ▶ $label = $self->_localize( $label ) if $self->localize_labels;
Branch: 1 branch
BranchTrueFalseTotal
if $self->localize_labels 0 5 5
27 5 ▶ my $attr = $option->{attributes} || {};
Condition100%2/2
$option->{'attributes'} || {}
l!l
14
MC/DC100%1/1
$option->{'attributes'} || {}
$option->{'attributes'}
Truth table100%2/2
A
A= $option->{'attributes'}
Aresult
11
00
28 5 my $attr_str = process_attrs($attr);
29 5 ▶ my $lattr = $option->{label_attributes} || {};
Condition100%2/2
$option->{'label_attributes'} || {}
l!l
14
MC/DC100%1/1
$option->{'label_attributes'} || {}
$option->{'label_attributes'}
Truth table100%2/2
A
A= $option->{'label_attributes'}
Aresult
11
00
30 5 my $lattr_str= process_attrs($lattr);
31 5 $output .= qq{\n<div$attr_str><label$lattr_str>$label</label>};
32 5 foreach my $group_opt ( @{ $option->{options} } ) {
33 15 $output .= $self->render_option( $group_opt, $result );
34 }
35 5 $output .= qq{\n</div>};
36 }
37 else {
38 25 $output .= $self->render_option( $option, $result );
39 }
40 }
41 10 $self->reset_options_index;
42 10 return $output;
43 }
44  
45 sub render_option {
46 40 ▶ my ( $self, $option, $result ) = @_;
Subroutine render_option: called CC=15 SCAR=28.3
Pod undocumented
47 40 ▶ $result ||= $self->result;
Condition33%1/3
$result ||= $self->result
l!l&&r!l&&!r
4000
MC/DC0%0/2
$result ||= $self->result
$result $self->result
Truth table33%1/3
A ||= B
A= $resultB= $self->result
ABresult
1X1
011
000
48  
49 # get existing values
50 40 my $fif = $result->fif;
51 40 my %fif_lookup;
52 40 ▶ @fif_lookup{@$fif} = () if $self->multiple;
Branch: 1 branch
BranchTrueFalseTotal
if $self->multiple 40 0 40
53  
54 # create option label attributes
55 40 ▶ my $lattr = $option->{label_attributes} || {};
Condition50%1/2
$option->{'label_attributes'} || {}
l!l
040
MC/DC0%0/1
$option->{'label_attributes'} || {}
$option->{'label_attributes'}
Truth table50%1/2
A
A= $option->{'label_attributes'}
Aresult
11
00
56 40 push @{ $lattr->{class} }, 'checkbox';
57 40 ▶ if ( $self->get_tag('inline') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_tag("inline")) 9 31 40
58 9 my $class = 'inline';
59 9 ▶ $class = 'checkbox-inline' if $self->has_flag('is_b3');
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_flag("is_b3") 3 6 9
60 9 push @{ $lattr->{class} }, $class;
61 }
62 40 my $lattr_str = process_attrs( $lattr );
63 40 my $id = $self->id . '.' . $self->options_index;
64 40 my $output = qq{\n<label$lattr_str for="$id">};
65 40 my $value = $option->{value};
66 40 $output .= qq{\n<input type="checkbox"};
67 40 $output .= qq{ value="} . $self->html_filter($value) . '"';
68 40 $output .= qq{ name="} . $self->html_name . '"';
69 40 $output .= qq{ id="$id"};
70  
71 # handle option attributes
72 40 ▶ my $attr = $option->{attributes} || {};
Condition50%1/2
$option->{'attributes'} || {}
l!l
040
MC/DC0%0/1
$option->{'attributes'} || {}
$option->{'attributes'}
Truth table50%1/2
A
A= $option->{'attributes'}
Aresult
11
00
73 40 ▶ if( defined $option->{disabled} && $option->{disabled} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $option->{"disabled"} and $option->{"disabled"}) 0 40 40
Condition33%1/3
defined $option->{"disabled"} and $option->{"disabled"}
!ll&&!rl&&r
4000
MC/DC0%0/2
defined $option->{"disabled"} and $option->{"disabled"}
defined $option->{"disabled"} $option->{"disabled"}
Truth table33%1/3
A and B
A= defined $option->{"disabled"}B= $option->{"disabled"}
ABresult
0X0
100
111
74 0 $attr->{disabled} = 'disabled';
75 }
76 40 ▶ if ( defined $fif &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fif and $self->multiple && exists $fif_lookup{$value} || $fif eq $value) 4 36 40
Condition55%5/9
defined $fif and $self->multiple && exists $fif_lookup{$value} || $fif eq $value
!ll&&!rl&&r
0400
$self->multiple and exists $fif_lookup{$value} or $fif eq $value
l!l&&r!l&&!r
4036
$self->multiple and exists $fif_lookup{$value}
!ll&&!rl&&r
0364
MC/DC0%0/4
defined $fif and $self->multiple && exists $fif_lookup{$value} || $fif eq $value
defined $fif $self->multiple exists $fif_lookup{$value} $fif eq $value
Truth table16%1/6
A and B and C or D
A= defined $fifB= $self->multipleC= exists $fif_lookup{$value}D= $fif eq $value
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
77 ( ( $self->multiple && exists $fif_lookup{$value} ) ||
78 ( $fif eq $value ) ) ) {
79 4 $attr->{checked} = 'checked';
80 }
81 40 $output .= process_attrs($attr);
82 40 $output .= " />\n";
83  
84 # handle label
85 40 my $label = $option->{label};
86 40 ▶ $label = $self->_localize($label) if $self->localize_labels;
Branch: 1 branch
BranchTrueFalseTotal
if $self->localize_labels 0 40 40
87 40 $output .= $self->html_filter($label);
88 40 $output .= "\n</label>";
89 40 $self->inc_options_index;
90  
91 40 ▶ if ($self->get_tag('checkbox_element_wrapper')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_tag("checkbox_element_wrapper")) 3 37 40
92 3 $output = qq{<div class="checkbox">$output</div>};
93 }
94 40 return $output;
95 }
96  
97 7 1;
98  
99 __END__
100  
101 =pod
102  
103 =encoding UTF-8
104  
105 =head1 NAME
106  
107 HTML::FormHandler::Widget::Field::CheckboxGroup - checkbox group field role
108  
109 =head1 VERSION
110  
111 version 0.410001
112  
113 =head1 SYNOPSIS
114  
115 Checkbox group widget for rendering multiple selects.
116  
117 Checkbox element label class is 'checkbox', plus 'inline'
118 if the 'inline' tag is set.
119  
120 Options hashrefs must have the keys 'value', and 'label'.
121 They may have an 'attributes' hashref key. The 'checked'
122 attribute should not be set in the options hashref. It should
123 be set by supplying a default value or from params input.
124  
125 See L<HTML::FormHandler::Field::Select> for documentation on
126 select fields and options.
127  
128 =head1 AUTHOR
129  
130 FormHandler Contributors - see HTML::FormHandler
131  
132 =head1 COPYRIGHT AND LICENSE
133  
134 This software is copyright (c) 2017 by Gerda Shank.
135  
136 This is free software; you can redistribute it and/or modify it under
137 the same terms as the Perl 5 programming language system itself.
138  
139 =cut
« FormHandler/Model/Object.pm ↑ Summary FormHandler/Field/Select.pm »