Valiant/HTML/FormBuilder/RadioButton.pm

filter: Statementstmt 88.5% 31 / 35 Branchbran 50.0% 7 / 14 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 85.7% 6 / 7 Podpod 25.0% 1 / 4 totaltotal 70.7% 46 / 65 CC 10 SCAR 18.4
CC
10
Cov
75%
__ANON__
10.1
default_theme
6.0
__ANON__
2.0
CRAP 6.3
« Valiant/Naming.pm ↑ Summary Valiant/Validator/Date.pm »
1 package Valiant::HTML::FormBuilder::RadioButton;
2  
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 extends 'Valiant::HTML::FormBuilder';
5  
6 1 has 'parent_builder' => (is=>'ro', required=>1);
7  
8 0 ▶sub default_theme {
Subroutine default_theme: not called CC=2 SCAR=17.9
Pod undocumented
9 0 my $self = shift;
10 0 ▶ return $self->parent_builder->can('default_theme') ?
Branch: 1 branch
BranchTrueFalseTotal
$self->parent_builder->can('default_theme') ? : 0 0 0
11 $self->parent_builder->default_theme :
12 +{};
13 }
14  
15 9 ▶sub text {
Subroutine text: called CC=1 SCAR=0.0
Pod undocumented
16 9 my $self = shift;
17 9 return $self->tag_value_for_attribute($self->options->{label_method});
18 }
19  
20 27 ▶sub value {
Subroutine value: called CC=1 SCAR=0.0
Pod undocumented
21 27 my $self = shift;
22 27 return $self->tag_value_for_attribute($self->options->{value_method});
23 }
24  
25 9 ▶sub checked {
Subroutine checked: called CC=1 SCAR=0.0
Pod documented
26 9 my $self = shift;
27 9 return $self->options->{checked};
28 }
29  
30 9 ▶around 'label', sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
31 9 my ($orig, $self, $attrs) = @_;
32 9 ▶ $attrs = +{} unless defined($attrs);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attrs 6 3 9
33 9 my $text = $self->human_name_for_label($self->text);
34 9 return $self->$orig($self->value, $attrs, $text);
35 1 };
36  
37 9 ▶around 'radio_button', sub {
Subroutine __ANON__: called CC=8 SCAR=23.2
38 9 my ($orig, $self, $attrs) = @_;
39 9 my $attribute = $self->options->{attribute};
40 9 ▶ $attrs = +{} unless defined($attrs);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attrs 6 3 9
41 9 $attrs->{name} = $self->name;
42 9 $attrs->{checked} = $self->checked;
43 9 $attrs->{id} = $self->tag_id_for_attribute($self->value);
44  
45 9 $attrs = $self->merge_theme_field_opts(radio_button=>$attrs->{attribute}, $attrs);
46  
47 9 my $has_error = 0;
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 ▶ $has_error = 1 if $error->bad_value eq $self->value;
Branch: 1 branch
BranchTrueFalseTotal
if $error->bad_value eq $self->value 0 0 0
51 }
52 }
53  
54 9 ▶ my $errors_classes = exists($attrs->{errors_classes}) ? delete($attrs->{errors_classes}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $attrs->{'errors_classes'} ? : 0 9 9
55 0 ▶ $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
56 if $errors_classes && $has_error;
57  
58 9 return $self->$orig($attribute, $self->value, $attrs);
59 1 };
60  
61 1 1;
62  
63 =head1 NAME
64  
65 Valiant::HTML::FormBuilder::RadioButton - A custom formbuilder for radio button content
66  
67 =head1 SYNOPSIS
68  
69 $fb_profile->collection_radio_buttons('state_id', $states, id=>'name', +{ errors_classes=>'is-invalid'}, sub (
70 my $fb_state = shift;
71 $fb_state->radio_button(+{class=>"form-check-input"});
72 $fb_state->label(+{class=>"form-check-label"});
73 });
74  
75 =head1 DESCRIPTION
76  
77 This is a custom subclass of L<Valiant::HTML::FormBuilder> that modifies the C<label> and
78 C<radio_button> method so that it is already aware of the the field values, if its checked or
79 not, etc. It also addes three methods, C<text> which is the text value of the collection;
80 C<value>, which is its value and C<checked> which is a boolean indicated if the checkbox is 'checked'
81 or not.
82  
83 Generally you use this inside a C<collection_radio_button> when you want to have some custom
84 HTML and attributes on the radio button and label elements (typically for styling as in the
85 example shown.
86  
87 Chances are you won't use this stand alone but rather from inside a parent builder.
88  
89 =head1 SEE ALSO
90
91 L<Valiant::HTML::FormBuilder>
92  
93 =head1 AUTHOR
94
95 See L<Valiant>
96  
97 =head1 COPYRIGHT & LICENSE
98
99 See L<Valiant>
100  
101 =cut
102  
« Valiant/Naming.pm ↑ Summary Valiant/Validator/Date.pm »