Valiant/HTML/FormBuilder/Checkbox.pm

filter: Statementstmt 83.7% 36 / 43 Branchbran 36.3% 8 / 22 Conditioncond 12.5% 1 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 8 / 8 Podpod 25.0% 1 / 4 totaltotal 60.0% 54 / 90 CC 16 SCAR 34.6
CC
16
Cov
62%
__ANON__
51.2
default_theme
2.1
__ANON__
2.0
CRAP 31.7
« Valiant/Validator/Collection.pm ↑ Summary Valiant/Validator/Numericality.pm »
1 package Valiant::HTML::FormBuilder::Checkbox;
2  
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Valiant::HTML::Util::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 extends 'Valiant::HTML::FormBuilder';
6  
7 1 has 'parent_builder' => (is=>'ro', required=>1);
8  
9 sub default_theme {
10 29 ▶ my $self = shift;
Subroutine default_theme: called CC=2 SCAR=7.2
Pod undocumented
11 29 ▶ return $self->parent_builder->can('default_theme') ?
Branch: 1 branch
BranchTrueFalseTotal
$self->parent_builder->can('default_theme') ? : 0 29 29
12 $self->parent_builder->default_theme :
13 +{};
14 }
15  
16 sub text {
17 9 ▶ my $self = shift;
Subroutine text: called CC=1 SCAR=0.0
Pod undocumented
18 9 return $self->tag_value_for_attribute($self->options->{label_method});
19 }
20  
21 sub value {
22 9 ▶ my $self = shift;
Subroutine value: called CC=1 SCAR=0.0
Pod undocumented
23 9 return $self->tag_value_for_attribute($self->options->{value_method});
24 }
25  
26 sub checked {
27 9 ▶ my $self = shift;
Subroutine checked: called CC=1 SCAR=0.0
Pod documented
28 9 return $self->options->{checked};
29 }
30  
31 around 'label', sub {
32 9 ▶ my ($orig, $self, $attrs) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
33 9 ▶ $attrs = +{} unless defined($attrs);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attrs 6 3 9
34 9 return $self->$orig($self->options->{attribute_value_method}, $attrs, $self->text);
35 1 };
36  
37 around 'checkbox', sub {
38 9 ▶ my ($orig, $self, $attrs) = @_;
Subroutine __ANON__: called CC=14 SCAR=39.3
39 9 ▶ $attrs = +{} unless defined($attrs);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attrs 6 3 9
40 9 $attrs->{name} = $self->tag_name_for_attribute($self->options->{attribute_value_method});
41 9 $attrs->{id} = $self->tag_id_for_attribute($self->options->{attribute_value_method});
42 9 $attrs->{include_hidden} = 0;
43 9 $attrs->{checked} = $self->checked;
44 9 $attrs = $self->merge_theme_field_opts(checkbox=>$attrs->{attribute}, $attrs);
45  
46 9 my $has_error = 0;
47 9 my $attribute_value_method = $self->options->{attribute_value_method};
48 9 ▶ if(my $errors = $self->options->{errors}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $errors = $self->options->{"errors"}) 9 0 9
49 9 foreach my $error(@$errors) {
50 0 my $bad_value_collection = $error->bad_value;
51 0 ▶ $bad_value_collection = Valiant::HTML::Util::Collection->new(@$bad_value_collection)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $bad_value_collection || "") eq "ARRAY" 0 0 0
Condition0%0/2
ref $bad_value_collection || ''
l!l
00
MC/DC0%0/1
ref $bad_value_collection || ''
ref $bad_value_collection
Truth table0%0/2
A
A= ref $bad_value_collection
Aresult
11
00
52 if (ref($bad_value_collection)||'') eq 'ARRAY';
53  
54 0 ▶ while(my $bad_value = $bad_value_collection->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $bad_value = $bad_value_collection->next) 0 0 0
55 0 ▶ next if $bad_value->can('is_marked_for_deletion') and $bad_value->is_marked_for_deletion;
Branch: 1 branch
BranchTrueFalseTotal
if $bad_value->can("is_marked_for_deletion") and $bad_value->is_marked_for_deletion 0 0 0
Condition0%0/3
$bad_value->can("is_marked_for_deletion") and $bad_value->is_marked_for_deletion
!ll&&!rl&&r
000
MC/DC0%0/2
$bad_value->can("is_marked_for_deletion") and $bad_value->is_marked_for_deletion
$bad_value->can("is_marked_for_deletion") $bad_value->is_marked_for_deletion
Truth table0%0/3
A and B
A= $bad_value->can("is_marked_for_deletion")B= $bad_value->is_marked_for_deletion
ABresult
0X0
100
111
56 0 ▶ $has_error = 1 if $bad_value->$attribute_value_method eq $self->value;
Branch: 1 branch
BranchTrueFalseTotal
if $bad_value->$attribute_value_method eq $self->value 0 0 0
57 }
58 0 ▶ $bad_value_collection->reset if $bad_value_collection->can('reset');
Branch: 1 branch
BranchTrueFalseTotal
if $bad_value_collection->can("reset") 0 0 0
59 }
60 }
61 9 ▶ my $errors_classes = exists($attrs->{errors_classes}) ? delete($attrs->{errors_classes}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $attrs->{'errors_classes'} ? : 0 9 9
62 9 ▶ $attrs->{class} = join(' ', (grep { defined $_ } $attrs->{class}, $errors_classes))
Branch: 1 branch
BranchTrueFalseTotal
if $errors_classes and $has_error 0 9 9
Condition33%1/3
$errors_classes and $has_error
!ll&&!rl&&r
900
MC/DC0%0/2
$errors_classes and $has_error
$errors_classes $has_error
Truth table33%1/3
A and B
A= $errors_classesB= $has_error
ABresult
0X0
100
111
63 if $errors_classes && $has_error;
64  
65 9 return $self->$orig($self->options->{value_method}, $attrs, $self->value);
66 1 };
67  
68  
69 1 1;
70  
71 =head1 NAME
72  
73 Valiant::HTML::FormBuilder::Checkbox - A custom formbuilder for checkbox content
74  
75 =head1 SYNOPSIS
76  
77 $fb->collection_checkbox({person_roles => 'role_id'}, $roles, id=>'label', sub {
78 my $fb_roles = shift;
79 $fb_roles->checkbox({class=>'form-check-input'});
80 $fb_roles->label({class=>'form-check-label'});
81 });
82  
83 =head1 DESCRIPTION
84  
85 This is a custom subclass of L<Valiant::HTML::FormBuilder> that modifies the C<label> and
86 C<checkbox> method so that it is already aware of the the field values, if its checked or
87 not, etc. It also addes three methods, C<text> which is the text value of the collection;
88 C<value>, which is its value and C<checked> which is a boolean indicated if the checkbox is 'checked'
89 or not.
90  
91 Generally you use this inside a C<collection_checkbox> when you want to have some custom
92 HTML and attributes on the checkbox and label elements (typically for styling as in the
93 example shown.
94  
95 Chances are you won't use this stand alone but rather from inside a parent builder.
96  
97 =head1 SEE ALSO
98
99 L<Valiant::HTML::FormBuilder>
100  
101 =head1 AUTHOR
102
103 See L<Valiant>
104  
105 =head1 COPYRIGHT & LICENSE
106
107 See L<Valiant>
108  
109 =cut
110  
« Valiant/Validator/Collection.pm ↑ Summary Valiant/Validator/Numericality.pm »