| 1 | use warnings; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | package Jifty::Web::Form::Field::Radio; | ||
| 5 | |||
| 6 | use base qw/Jifty::Web::Form::Field/; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Jifty::Web::Form::Field::Radio - Add a radio button to your forms | ||
| 11 | |||
| 12 | =head1 METHODS | ||
| 13 | |||
| 14 | =head2 render_widget | ||
| 15 | |||
| 16 | Renders the whole radio button collection. | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | sub render_widget { | ||
| 21 | my $self = shift; | ||
| 22 | |||
| 23 | for my $opt ($self->available_values) { | ||
| 24 | $self->render_option($opt); | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | =head2 render_label | ||
| 29 | |||
| 30 | We need to output the label as a span instead since the labels are associated | ||
| 31 | with the individual options. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | sub render_label { | ||
| 36 | my $self = shift; | ||
| 37 | Jifty->web->out( | ||
| 38 | qq!<span class="label @{[$self->classes]}">@{[_($self->label) ]}</span>\n! | ||
| 39 | ); | ||
| 40 | |||
| 41 | return ''; | ||
| 42 | } | ||
| 43 | |||
| 44 | =head2 render_option option | ||
| 45 | |||
| 46 | Renders a radio widget | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub render_option { | ||
| 51 | my $self = shift; | ||
| 52 | my $opt = shift; | ||
| 53 | my $display = ref($opt) ? $opt->{'display'} : $opt; | ||
| 54 | my $value = ref($opt) ? $opt->{'value'} : $opt; | ||
| 55 | $value = '' if !defined($value); | ||
| 56 | |||
| 57 | my $id = $self->element_id . "-" . $value; | ||
| 58 | $id =~ s/\s+/_/; | ||
| 59 | my $field = qq! <input type="radio" !; | ||
| 60 | $field .= qq! name="@{[ $self->input_name ]}"!; | ||
| 61 | $field .= qq! id="@{[ $id ]}"!; | ||
| 62 | $field .= qq! title="@{[ $self->title ]}"! if ($self->title); | ||
| 63 | $field .= qq! value="@{[ $value ]}"!; | ||
| 64 | $field .= $self->_widget_class; | ||
| 65 | |||
| 66 | $field .= qq! checked="checked" ! | ||
| 67 | if defined $self->current_value and $self->current_value eq $value; | ||
| 68 | $field .= $self->javascript; | ||
| 69 | $field .= qq! /><label for="@{[ $id ]}"!; | ||
| 70 | $field .= $self->_widget_class; | ||
| 71 | $field .= qq!>$display</label>\n!; | ||
| 72 | $field = qq{<span class="radiooption">$field</span>}; | ||
| 73 | |||
| 74 | Jifty->web->out($field); | ||
| 75 | ''; | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | 1; |