Valiant/HTML/FormBuilder/RadioButton.pm

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