Widget/Element/Radio.pm untested

filter: Statementstmt 0.0% 0 / 38 Branchbran 0.0% 0 / 14 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 / 90 CC 8 SCAR 38.1
CC
8
Cov
0%
containerize
72.0
new
2.0
BEGIN
2.0
CRAP 45.1
1 package HTML::Widget::Element::Radio;
2  
3 use warnings;
4 use strict;
5 use base 'HTML::Widget::Element';
6 use NEXT;
7  
8 __PACKAGE__->mk_accessors(qw/comment checked label value retain_default/);
9  
10 =head1 NAME
11  
12 HTML::Widget::Element::Radio - Radio Element
13  
14 =head1 SYNOPSIS
15  
16 my $e = $widget->element( 'Radio', 'foo' );
17 $e->comment('(Required)');
18 $e->label('Foo');
19 $e->checked('checked');
20 $e->value('bar');
21  
22 =head1 DESCRIPTION
23  
24 Radio 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 radio button 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::Radio') ) {
60 $multi++;
61 }
62 }
63  
64 # Generate unique id
65 if ($multi) {
66 $w->{_stash} ||= {};
67 $w->{_stash}->{radio} ||= {};
68 my $num = ++$w->{_stash}->{radio}->{$name};
69 my $id = $self->id( $w, "$name\_$num" );
70 $self->attributes->{id} ||= $id;
71 }
72  
73 my $checked =
74 defined $value
75 ? ( defined $value && $value eq $self->value )
76 ? 'checked'
77 : undef
78 : undef;
79  
80 if ( !defined $value
81 && ( $self->retain_default || !$args->{submitted} )
82 && $self->checked )
83 {
84 $checked = 'checked';
85 }
86 $value = $self->value;
87  
88 my $l = $self->mk_label( $w, $self->label, $self->comment, $errors );
89 my $i = $self->mk_input( $w,
90 { checked => $checked, type => 'radio', value => $value }, $errors );
91  
92 #$l ? ( $l->unshift_content($i) ) : ( $l = $i );
93 my $e = $self->mk_error( $w, $errors );
94  
95 return $self->container( { element => $i, error => $e, label => $l } );
96 }
97  
98 =head1 SEE ALSO
99  
100 L<HTML::Widget::Element>
101  
102 =head1 AUTHOR
103  
104 Sebastian Riedel, C<sri@oook.de>
105  
106 =head1 LICENSE
107  
108 This library is free software, you can redistribute it and/or modify it under
109 the same terms as Perl itself.
110  
111 =cut
112  
113 1;