Widget/Element/Checkbox.pm untested

filter: Statementstmt 0.0% 0 / 38 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 88 CC 7 SCAR 35.1
CC
7
Cov
0%
containerize
56.0
new
2.0
BEGIN
2.0
CRAP 33.5
1 package HTML::Widget::Element::Checkbox;
2  
3 use warnings;
4 use strict;
5 use base 'HTML::Widget::Element';
6 use NEXT;
7  
8 __PACKAGE__->mk_accessors(qw/checked comment label value retain_default/);
9  
10 =head1 NAME
11  
12 HTML::Widget::Element::Checkbox - Checkbox Element
13  
14 =head1 SYNOPSIS
15  
16 my $e = $widget->element( 'Checkbox', 'foo' );
17 $e->comment('(Required)');
18 $e->label('Foo');
19 $e->checked('checked');
20 $e->value('bar');
21  
22 =head1 DESCRIPTION
23  
24 Checkbox Element.
25  
26 =head1 METHODS
27  
28 =head2 retain_default
29  
30 If true, overrides the default behaviour, so that after a field is missing
31 from the form submission, the xml output will contain the default value,
32 rather than be empty.
33  
34 =head2 new
35  
36 =cut
37  
38 sub new {
39 shift->NEXT::new(@_)->value(1);
40 }
41  
42 =head2 containerize
43  
44 =cut
45  
46 sub containerize {
47 my ( $self, $w, $value, $errors, $args ) = @_;
48  
49 $value = ref $value eq 'ARRAY' ? shift @$value : $value;
50  
51 my $name = $self->name;
52  
53 # Search for multiple checkboxes with the same name
54 my $multi = 0;
55 my @elements = $w->find_elements( name => $name );
56  
57 for my $element (@elements) {
58 next if $element eq $self;
59 if ( $element->isa('HTML::Widget::Element::Checkbox') ) {
60 $multi++;
61 }
62 }
63  
64 # Generate unique id
65 if ($multi) {
66 $w->{_stash} ||= {};
67 $w->{_stash}->{checkbox} ||= {};
68 my $num = ++$w->{_stash}->{checkbox}->{$name};
69 my $id = $self->id( $w, "$name\_$num" );
70 $self->attributes->{id} ||= $id;
71 }
72  
73 my $checked
74 = ( defined $value && $value eq $self->value ) ? 'checked' : undef;
75  
76 if ( !defined $value
77 && ( $self->retain_default || !$args->{submitted} )
78 && $self->checked )
79 {
80 $checked = 'checked';
81 }
82 $value = $self->value;
83  
84 my $l = $self->mk_label( $w, $self->label, $self->comment, $errors );
85 my $i = $self->mk_input( $w,
86 { checked => $checked, type => 'checkbox', value => $value }, $errors );
87 my $e = $self->mk_error( $w, $errors );
88  
89 return $self->container( { element => $i, error => $e, label => $l } );
90 }
91  
92 =head1 SEE ALSO
93  
94 L<HTML::Widget::Element>
95  
96 =head1 AUTHOR
97  
98 Sebastian Riedel, C<sri@oook.de>
99  
100 =head1 LICENSE
101  
102 This library is free software, you can redistribute it and/or modify it under
103 the same terms as Perl itself.
104  
105 =cut
106  
107 1;