| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 24 |  |  | 24 |  | 7107 | use strict; | 
|  | 24 |  |  |  |  | 56 |  | 
|  | 24 |  |  |  |  | 1719 |  | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | package HTML::FormFu::Element::Checkbox; | 
| 4 |  |  |  |  |  |  | $HTML::FormFu::Element::Checkbox::VERSION = '2.07'; | 
| 5 |  |  |  |  |  |  | # ABSTRACT: Checkbox form field | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 24 |  |  | 24 |  | 567 | use Moose; | 
|  | 24 |  |  |  |  | 57 |  | 
|  | 24 |  |  |  |  | 161 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | extends 'HTML::FormFu::Element'; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | with 'HTML::FormFu::Role::Element::Input'; | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 24 |  |  | 24 |  | 179430 | use HTML::FormFu::Constants qw( $EMPTY_STR ); | 
|  | 24 |  |  |  |  | 58 |  | 
|  | 24 |  |  |  |  | 11192 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | __PACKAGE__->mk_output_accessors(qw( default )); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | after BUILD => sub { | 
| 18 |  |  |  |  |  |  | my ( $self, $args ) = @_; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | $self->field_type('checkbox'); | 
| 21 |  |  |  |  |  |  | $self->value(1); | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | $self->multi_layout( [ 'field', 'label', ] ); | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | return; | 
| 26 |  |  |  |  |  |  | }; | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | sub process_value { | 
| 29 | 104 |  |  | 104 |  | 244 | my ( $self, $input ) = @_; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | # ignore submitted input | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 104 |  |  |  |  | 330 | return $self->value; | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | sub prepare_attrs { | 
| 37 | 104 |  |  | 104 |  | 254 | my ( $self, $render ) = @_; | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 104 |  |  |  |  | 360 | my $form      = $self->form; | 
| 40 | 104 |  |  |  |  | 2814 | my $submitted = $form->submitted; | 
| 41 | 104 |  |  |  |  | 466 | my $default   = $self->default; | 
| 42 | 104 |  |  |  |  | 340 | my $original  = $self->value; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 104 | 50 |  |  |  | 341 | my $value | 
| 45 |  |  |  |  |  |  | = defined $self->name | 
| 46 |  |  |  |  |  |  | ? $self->get_nested_hash_value( $form->input, $self->nested_name ) | 
| 47 |  |  |  |  |  |  | : undef; | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 104 | 100 | 100 |  |  | 481 | if ( defined $value and ref $value eq 'ARRAY' ) { | 
| 50 | 2 | 50 |  |  |  | 6 | $value = $original if grep { $_ eq $original } @$value; | 
|  | 4 |  |  |  |  | 12 |  | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 104 | 100 | 100 |  |  | 2304 | if (   $submitted | 
|  |  | 100 | 66 |  |  |  |  | 
|  |  | 100 | 100 |  |  |  |  | 
|  |  | 100 | 100 |  |  |  |  | 
|  |  |  | 66 |  |  |  |  | 
|  |  |  | 66 |  |  |  |  | 
|  |  |  | 66 |  |  |  |  | 
|  |  |  | 100 |  |  |  |  | 
| 54 |  |  |  |  |  |  | && defined $value | 
| 55 |  |  |  |  |  |  | && defined $original | 
| 56 |  |  |  |  |  |  | && $value eq $original ) | 
| 57 |  |  |  |  |  |  | { | 
| 58 | 23 |  |  |  |  | 77 | $render->{attributes}{checked} = 'checked'; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  | elsif ($submitted | 
| 61 |  |  |  |  |  |  | && $self->retain_default | 
| 62 |  |  |  |  |  |  | && ( !defined $value || $value eq $EMPTY_STR ) ) | 
| 63 |  |  |  |  |  |  | { | 
| 64 | 8 |  |  |  |  | 69 | $render->{attributes}{checked} = 'checked'; | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  | elsif ($submitted) { | 
| 67 | 34 |  |  |  |  | 96 | delete $render->{attributes}{checked}; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | elsif ( defined $default && defined $original && $default eq $original ) { | 
| 70 | 11 |  |  |  |  | 39 | $render->{attributes}{checked} = 'checked'; | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 104 |  |  |  |  | 545 | $self->SUPER::prepare_attrs($render); | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 104 |  |  |  |  | 248 | return; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable; | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | 1; | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | __END__ | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | =pod | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | =encoding UTF-8 | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | =head1 NAME | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | HTML::FormFu::Element::Checkbox - Checkbox form field | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | =head1 VERSION | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | version 2.07 | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | my $e = $form->element( Checkbox => 'foo' ); | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | Checkbox form field. | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | =head1 METHODS | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | =head2 value | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | Default Value: 1 | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =head2 default_empty_value | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | Inherited. See L<HTML::FormFu::Role::Element::Field/default_empty_value> for details. | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | =head2 multi_layout | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | Overrides the default value of | 
| 117 |  |  |  |  |  |  | L<multi_layout|HTML::FormFu::Role::Element::Field/multi_layout> | 
| 118 |  |  |  |  |  |  | to swap the C<field> and C<label> around. | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | Is a sub-class of, and inherits methods from | 
| 123 |  |  |  |  |  |  | L<HTML::FormFu::Role::Element::Input>, | 
| 124 |  |  |  |  |  |  | L<HTML::FormFu::Role::Element::Field>, | 
| 125 |  |  |  |  |  |  | L<HTML::FormFu::Element> | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | L<HTML::FormFu> | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | =head1 AUTHOR | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | Carl Franks, C<cfranks@cpan.org> | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | =head1 LICENSE | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | This library is free software, you can redistribute it and/or modify it under | 
| 136 |  |  |  |  |  |  | the same terms as Perl itself. | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | =head1 AUTHOR | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | Carl Franks <cpan@fireartist.com> | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | This software is copyright (c) 2018 by Carl Franks. | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 147 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | =cut |