| 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 | 0 | ▶ | sub default_theme { | ||||||||||||||||||||||||||
|
Subroutine
default_theme: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 10 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 11 | 0 | ▶ | return $self->parent_builder->can('default_theme') ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 12 | $self->parent_builder->default_theme : | ||||||||||||||||||||||||||||
| 13 | +{}; | ||||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 15 | ▶ | sub text { | ||||||||||||||||||||||||||
|
Subroutine
text: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | 15 | my $self = shift; | |||||||||||||||||||||||||||
| 18 | 15 | return $self->tag_value_for_attribute($self->options->{label_method}); | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 45 | ▶ | sub value { | ||||||||||||||||||||||||||
|
Subroutine
value: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 45 | my $self = shift; | |||||||||||||||||||||||||||
| 23 | 45 | return $self->tag_value_for_attribute($self->options->{value_method}); | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 15 | ▶ | sub checked { | ||||||||||||||||||||||||||
|
Subroutine
checked: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 15 | my $self = shift; | |||||||||||||||||||||||||||
| 28 | 15 | return $self->options->{checked}; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | #around 'label', sub { | ||||||||||||||||||||||||||||
| 32 | # my ($orig, $self, $attrs) = @_; | ||||||||||||||||||||||||||||
| 33 | # $attrs = +{} unless defined($attrs); | ||||||||||||||||||||||||||||
| 34 | # return $self->$orig($self->options->{attribute_value_method}, $attrs, $self->text); | ||||||||||||||||||||||||||||
| 35 | #}; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 15 | ▶ | around 'label', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 38 | 15 | my ($orig, $self, $attrs) = @_; | |||||||||||||||||||||||||||
| 39 | 15 | ▶ | $attrs = +{} unless defined($attrs); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 15 | my $text = $self->human_name_for_label($self->text); | |||||||||||||||||||||||||||
| 41 | 15 | return $self->$orig($self->value, $attrs, $text); | |||||||||||||||||||||||||||
| 42 | 1 | }; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 15 | ▶ | around 'checkbox', sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=15 SCAR=39.2
| |||||||||||||||||||||||||||||
| 45 | 15 | my ($orig, $self, $attrs) = @_; | |||||||||||||||||||||||||||
| 46 | 15 | ▶ | $attrs = +{} unless defined($attrs); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | #$attrs->{name} = $self->tag_name_for_attribute($self->options->{attribute_value_method}); | ||||||||||||||||||||||||||||
| 48 | 15 | $attrs->{name} = $self->name; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 15 | $attrs->{id} = $self->tag_id_for_attribute($self->value); # $self->options->{attribute_value_method} | |||||||||||||||||||||||||||
| 51 | 15 | $attrs->{include_hidden} = 0; | |||||||||||||||||||||||||||
| 52 | 15 | $attrs->{checked} = $self->checked; | |||||||||||||||||||||||||||
| 53 | 15 | $attrs = $self->merge_theme_field_opts(checkbox=>$attrs->{attribute}, $attrs); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 15 | my $has_error = 0; | |||||||||||||||||||||||||||
| 56 | 15 | my $attribute_value_method = $self->options->{attribute_value_method}; | |||||||||||||||||||||||||||
| 57 | 15 | ▶ | if(my $errors = $self->options->{errors}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 15 | foreach my $error(@$errors) { | |||||||||||||||||||||||||||
| 59 | 0 | my $bad_value_collection = $error->bad_value; | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | $bad_value_collection = Valiant::HTML::Util::Collection->new(@$bad_value_collection) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
ref $bad_value_collection || ''
MC/DC0%0/1
ref $bad_value_collection || ''
ref $bad_value_collection
Truth table0%0/2
A
A= ref $bad_value_collection
| |||||||||||||||||||||||||||||
| 61 | if (ref($bad_value_collection)||'') eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | while(my $bad_value = $bad_value_collection->next) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | next if $bad_value->can('is_marked_for_deletion') and $bad_value->is_marked_for_deletion; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$bad_value->can("is_marked_for_deletion") and $bad_value->is_marked_for_deletion
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
| |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | $has_error = 1 if $bad_value->$attribute_value_method eq $self->value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | $bad_value_collection->reset if $bad_value_collection->can('reset'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 15 | ▶ | my $errors_classes = exists($attrs->{errors_classes}) ? delete($attrs->{errors_classes}) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | $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
| |||||||||||||||||||||||||||||
| 72 | if $errors_classes && $has_error; | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | my $value = $self->options->{is_spec_attribute} ? | ||||||||||||||||||||||||||||
| 75 | 15 | ▶ | +{$self->options->{attribute_value_method} => $self->value}: | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | $self->value; | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 15 | return $self->$orig($self->options->{value_method}, $attrs, $value); | |||||||||||||||||||||||||||
| 79 | 1 | }; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1 | 1; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head1 NAME | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | Valiant::HTML::FormBuilder::Checkbox - A custom formbuilder for checkbox content | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | $fb->collection_checkbox({person_roles => 'role_id'}, $roles, id=>'label', sub { | ||||||||||||||||||||||||||||
| 91 | my $fb_roles = shift; | ||||||||||||||||||||||||||||
| 92 | $fb_roles->checkbox({class=>'form-check-input'}); | ||||||||||||||||||||||||||||
| 93 | $fb_roles->label({class=>'form-check-label'}); | ||||||||||||||||||||||||||||
| 94 | }); | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | This is a custom subclass of L<Valiant::HTML::FormBuilder> that modifies the C<label> and | ||||||||||||||||||||||||||||
| 99 | C<checkbox> method so that it is already aware of the the field values, if its checked or | ||||||||||||||||||||||||||||
| 100 | not, etc. It also addes three methods, C<text> which is the text value of the collection; | ||||||||||||||||||||||||||||
| 101 | C<value>, which is its value and C<checked> which is a boolean indicated if the checkbox is 'checked' | ||||||||||||||||||||||||||||
| 102 | or not. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Generally you use this inside a C<collection_checkbox> when you want to have some custom | ||||||||||||||||||||||||||||
| 105 | HTML and attributes on the checkbox and label elements (typically for styling as in the | ||||||||||||||||||||||||||||
| 106 | example shown. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Chances are you won't use this stand alone but rather from inside a parent builder. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | L<Valiant::HTML::FormBuilder> | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | See L<Valiant> | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | See L<Valiant> | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||