/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

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