PageRenderTime 21ms CodeModel.GetById 14ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/HTML/FormHandler/Field/Checkbox.pm

http://github.com/gshank/html-formhandler
Perl | 57 lines | 42 code | 14 blank | 1 comment | 8 complexity | 54f343e2604ebb97f5cc1f07a5cfc9db MD5 | raw file
 1package HTML::FormHandler::Field::Checkbox;
 2# ABSTRACT: a checkbox field type
 3
 4use HTML::FormHandler::Moose;
 5extends 'HTML::FormHandler::Field';
 6
 7=head1 DESCRIPTION
 8
 9This field is very similar to the Boolean Widget except that this
10field allows other positive values besides 1. Since unselected
11checkboxes do not return a parameter, fields with Checkbox type
12will always be set to the 'input_without_param' default if they
13do not appear in the form.
14
15=head2 widget
16
17checkbox
18
19=head2 checkbox_value
20
21In order to create the HTML for a checkbox, there must be a 'value="xx"'.
22This value is specified with the 'checkbox_value' attribute, which
23defaults to 1.
24
25=head2 input_without_param
26
27If the checkbox is not checked, it will be set to the value
28of this attribute (the unchecked value). Default = 0. Because
29unchecked checkboxes do not return anything in the HTTP parameters,
30the absence of a checkbox key in the parameters hash forces this
31field to this value. This means that Checkbox fields, unlike other
32fields, will not be ignored if there is no input. If a particular
33checkbox should not be processed for a particular form, you must
34set 'inactive' to 1 instead.
35
36Note that a checkbox is only 'checked' when the 'checkbox_value' is
37provided. The 'value' for a non-checked checkbox is only really
38useful for creating form values such as are stored in a database.
39
40=cut
41
42has '+widget'              => ( default => 'Checkbox' );
43has 'checkbox_value'       => ( is      => 'rw', default => 1 );
44has '+input_without_param' => ( default => 0 );
45has '+type_attr'           => ( default => 'checkbox' );
46has 'option_label'         => ( is => 'rw' );
47has 'option_wrapper'       => ( is => 'rw' );
48
49sub validate {
50    my $self = shift;
51    $self->add_error($self->get_message('required'), $self->loc_label) if( $self->required && !$self->value );
52    return;
53}
54
55__PACKAGE__->meta->make_immutable;
56use namespace::autoclean;
571;