File Coverage

blib/lib/HTML/FormHandler/Field/Checkbox.pm
Criterion Covered Total %
statement 9 9 100.0
branch 2 2 100.0
condition 3 3 100.0
subroutine 3 3 100.0
pod 1 1 100.0
total 18 18 100.0


line stmt bran cond sub pod time code
1             package HTML::FormHandler::Field::Checkbox;
2             # ABSTRACT: a checkbox field type
3             $HTML::FormHandler::Field::Checkbox::VERSION = '0.40067';
4 25     25   17483 use HTML::FormHandler::Moose;
  25         35  
  25         215  
5             extends 'HTML::FormHandler::Field';
6              
7              
8             has '+widget' => ( default => 'Checkbox' );
9             has 'checkbox_value' => ( is => 'rw', default => 1 );
10             has '+input_without_param' => ( default => 0 );
11             has '+type_attr' => ( default => 'checkbox' );
12             has 'option_label' => ( is => 'rw' );
13             has 'option_wrapper' => ( is => 'rw' );
14              
15             sub validate {
16 50     50 1 71 my $self = shift;
17 50 100 100     1229 $self->add_error($self->get_message('required'), $self->loc_label) if( $self->required && !$self->value );
18 50         86 return;
19             }
20              
21             __PACKAGE__->meta->make_immutable;
22 25     25   41964 use namespace::autoclean;
  25         43  
  25         217  
23             1;
24              
25             __END__
26              
27             =pod
28              
29             =encoding UTF-8
30              
31             =head1 NAME
32              
33             HTML::FormHandler::Field::Checkbox - a checkbox field type
34              
35             =head1 VERSION
36              
37             version 0.40067
38              
39             =head1 DESCRIPTION
40              
41             This field is very similar to the Boolean Widget except that this
42             field allows other positive values besides 1. Since unselected
43             checkboxes do not return a parameter, fields with Checkbox type
44             will always be set to the 'input_without_param' default if they
45             do not appear in the form.
46              
47             =head2 widget
48              
49             checkbox
50              
51             =head2 checkbox_value
52              
53             In order to create the HTML for a checkbox, there must be a 'value="xx"'.
54             This value is specified with the 'checkbox_value' attribute, which
55             defaults to 1.
56              
57             =head2 input_without_param
58              
59             If the checkbox is not checked, it will be set to the value
60             of this attribute (the unchecked value). Default = 0. Because
61             unchecked checkboxes do not return anything in the HTTP parameters,
62             the absence of a checkbox key in the parameters hash forces this
63             field to this value. This means that Checkbox fields, unlike other
64             fields, will not be ignored if there is no input. If a particular
65             checkbox should not be processed for a particular form, you must
66             set 'inactive' to 1 instead.
67              
68             Note that a checkbox is only 'checked' when the 'checkbox_value' is
69             provided. The 'value' for a non-checked checkbox is only really
70             useful for creating form values such as are stored in a database.
71              
72             =head1 AUTHOR
73              
74             FormHandler Contributors - see HTML::FormHandler
75              
76             =head1 COPYRIGHT AND LICENSE
77              
78             This software is copyright (c) 2016 by Gerda Shank.
79              
80             This is free software; you can redistribute it and/or modify it under
81             the same terms as the Perl 5 programming language system itself.
82              
83             =cut