File: RadioGroup.pm

package info (click to toggle)
libhtml-formhandler-perl 0.40013-1%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,136 kB
  • sloc: perl: 8,102; makefile: 2
file content (127 lines) | stat: -rw-r--r-- 3,567 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package HTML::FormHandler::Widget::Field::RadioGroup;
# ABSTRACT: radio group rendering widget
use Moose::Role;
use namespace::autoclean;
use HTML::FormHandler::Render::Util ('process_attrs');


sub render {
    my ( $self, $result ) = @_;
    $result ||= $self->result;
    my $output = $self->render_element( $result );
    return $self->wrap_field( $result, $output );
}

sub render_element {
    my ( $self, $result ) = @_;
    $result ||= $self->result;

    my $output = '';
    $output .= "<br />" if $self->get_tag('radio_br_after');

    foreach my $option ( @{ $self->{options} } ) {
        if ( my $label = $option->{group} ) {
            $label = $self->_localize( $label ) if $self->localize_labels;
            my $attr = $option->{attributes} || {};
            my $attr_str = process_attrs($attr);
            my $lattr = $option->{label_attributes} || {};
            my $lattr_str= process_attrs($attr);
            $output .= qq{\n<div$attr_str><label$lattr_str>$label</label>};
            foreach my $group_opt ( @{ $option->{options} } ) {
                $output .= $self->render_option( $group_opt, $result );
            }
            $output .= qq{\n</div>};
        }
        else {
            $output .= $self->render_option( $option, $result );
        }
        $output .= '<br />' if $self->get_tag('radio_br_after');
    }
    $self->reset_options_index;
    return $output;
}

sub render_option {
    my ( $self, $option, $result ) = @_;

    $result ||= $result;
    my $rendered_widget = $self->render_radio( $result, $option );
    my $output = $self->wrap_radio( $rendered_widget, $option->{label} );
    $self->inc_options_index;
    return $output;
}

sub render_wrapped_option {
    my ( $self, $option, $result ) = @_;

    $result ||= $self->result;
    my $output = $self->render_option( $option, $result );
    return $self->wrap_field( $result, $output );
}

sub render_radio {
    my ( $self, $result, $option ) = @_;
    $result ||= $self->result;

    my $value = $option->{value};
    my $id = $self->id . "." . $self->options_index;
    my $output = '<input type="radio" name="'
        . $self->html_name . qq{" id="$id" value="}
        . $self->html_filter($value) . '"';
    $output .= ' checked="checked"'
        if $result->fif eq $value;
    $output .= process_attrs($option->{attributes});
    $output .= ' />';
    return $output;
}

sub wrap_radio {
    my ( $self, $rendered_widget, $option_label ) = @_;

    my $id = $self->id . "." . $self->options_index;
    my $for = qq{ for="$id"};

    # use "simple" label attributes for inner label
    my @label_class = ('radio');
    push @label_class, 'inline' if $self->get_tag('inline');
    my $lattrs = process_attrs( { class => \@label_class } );

    # return wrapped radio, either on left or right
    my $label = $self->_localize($option_label);
    return qq{<label$lattrs$for>\n$label\n$rendered_widget</label>}
        if( $self->get_tag('label_left') );
    return qq{<label$lattrs$for>$rendered_widget\n$label\n</label>};
}

1;

__END__
=pod

=head1 NAME

HTML::FormHandler::Widget::Field::RadioGroup - radio group rendering widget

=head1 VERSION

version 0.40013

=head1 SYNOPSIS

Renders a radio group (from a 'Select' field);

Tags: radio_br_after

=head1 AUTHOR

FormHandler Contributors - see HTML::FormHandler

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Gerda Shank.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut