| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 46 | 9 | foreach my $error(@$errors) { | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | $has_error = 1 if $error->bad_value eq $self->value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 9 | ▶ | my $errors_classes = exists($attrs->{errors_classes}) ? delete($attrs->{errors_classes}) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 9 | ▶ | $attrs->{class} = join(' ', (grep { defined $_ } $attrs->{class}, $errors_classes)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$errors_classes and $has_error
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
| |||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||