| 1 |
package HTML::FormHandler::Widget::Field::RadioGroup; |
| 2 |
# ABSTRACT: radio group rendering widget |
| 3 |
$HTML::FormHandler::Widget::Field::RadioGroup::VERSION = '0.410002'; |
| 4 |
use Moose::Role; |
| 5 |
use namespace::autoclean; |
| 6 |
use HTML::FormHandler::Render::Util ('process_attrs'); |
| 7 |
use HTML::Entities qw( encode_entities ); |
| 8 |
|
| 9 |
|
| 10 |
sub type_attr { 'radio' } |
| 11 |
|
| 12 |
sub render { |
| 13 |
my ( $self, $result ) = @_; |
| 14 |
$result ||= $self->result; |
| 15 |
die "No result for form field '" . $self->full_name . "'. Field may be inactive." unless $result; |
| 16 |
my $output = $self->render_element( $result ); |
| 17 |
return $self->wrap_field( $result, $output ); |
| 18 |
} |
| 19 |
|
| 20 |
sub render_element { |
| 21 |
my ( $self, $result ) = @_; |
| 22 |
$result ||= $self->result; |
| 23 |
|
| 24 |
my $output = ''; |
| 25 |
$output .= "<br />" if $self->get_tag('radio_br_after'); |
| 26 |
|
| 27 |
foreach my $option ( @{ $self->{options} } ) { |
| 28 |
if ( my $label = $option->{group} ) { |
| 29 |
$label = $self->_localize( $label ) if $self->localize_labels; |
| 30 |
my $attr = $option->{attributes} || {}; |
| 31 |
my $attr_str = process_attrs($attr); |
| 32 |
my $lattr = $option->{label_attributes} || {}; |
| 33 |
my $lattr_str= process_attrs($lattr); |
| 34 |
$output .= qq{\n<div$attr_str><label$lattr_str>} . encode_entities($label, '<>&') . qq{</label>}; |
| 35 |
foreach my $group_opt ( @{ $option->{options} } ) { |
| 36 |
$output .= $self->render_option( $group_opt, $result ); |
| 37 |
} |
| 38 |
$output .= qq{\n</div>}; |
| 39 |
} |
| 40 |
else { |
| 41 |
$output .= $self->render_option( $option, $result ); |
| 42 |
} |
| 43 |
$output .= '<br />' if $self->get_tag('radio_br_after'); |
| 44 |
} |
| 45 |
$self->reset_options_index; |
| 46 |
return $output; |
| 47 |
} |
| 48 |
|
| 49 |
sub render_option { |
| 50 |
my ( $self, $option, $result ) = @_; |
| 51 |
|
| 52 |
$result ||= $result; |
| 53 |
my $rendered_widget = $self->render_radio( $result, $option ); |
| 54 |
my $output = $self->wrap_radio( $rendered_widget, $option->{label} ); |
| 55 |
$self->inc_options_index; |
| 56 |
return $output; |
| 57 |
} |
| 58 |
|
| 59 |
sub render_wrapped_option { |
| 60 |
my ( $self, $option, $result ) = @_; |
| 61 |
|
| 62 |
$result ||= $self->result; |
| 63 |
my $output = $self->render_option( $option, $result ); |
| 64 |
return $self->wrap_field( $result, $output ); |
| 65 |
} |
| 66 |
|
| 67 |
sub render_radio { |
| 68 |
my ( $self, $result, $option ) = @_; |
| 69 |
$result ||= $self->result; |
| 70 |
|
| 71 |
my $value = $option->{value}; |
| 72 |
my $id = $self->id . "." . $self->options_index; |
| 73 |
my $output = '<input type="radio" name="' |
| 74 |
. $self->html_name . qq{" id="$id" value="} |
| 75 |
. $self->html_filter($value) . '"'; |
| 76 |
$output .= ' checked="checked"' |
| 77 |
if $result->fif eq $value; |
| 78 |
$output .= process_attrs($option->{attributes}); |
| 79 |
$output .= ' />'; |
| 80 |
return $output; |
| 81 |
} |
| 82 |
|
| 83 |
sub wrap_radio { |
| 84 |
my ( $self, $rendered_widget, $option_label ) = @_; |
| 85 |
|
| 86 |
my $id = $self->id . "." . $self->options_index; |
| 87 |
my $for = qq{ for="$id"}; |
| 88 |
|
| 89 |
# use "simple" label attributes for inner label |
| 90 |
my @label_class = ('radio'); |
| 91 |
if ( $self->get_tag('inline') ) { |
| 92 |
my $class = 'inline'; |
| 93 |
$class = 'radio-inline' if $self->has_flag('is_b3'); |
| 94 |
push @label_class, $class; |
| 95 |
} |
| 96 |
my $lattrs = process_attrs( { class => \@label_class } ); |
| 97 |
|
| 98 |
# return wrapped radio, either on left or right |
| 99 |
my $label = encode_entities( $self->_localize($option_label), '<>&' ); |
| 100 |
my $output = ''; |
| 101 |
if ( $self->get_tag('label_left') ) { |
| 102 |
$output = qq{<label$lattrs$for>\n$label\n$rendered_widget</label>}; |
| 103 |
} |
| 104 |
else { |
| 105 |
$output = qq{<label$lattrs$for>$rendered_widget\n$label\n</label>}; |
| 106 |
} |
| 107 |
if ( $self->get_tag('radio_element_wrapper') ) { |
| 108 |
$output = qq{<div class="radio">$output</div>}; |
| 109 |
} |
| 110 |
return $output; |
| 111 |
} |
| 112 |
|
| 113 |
1; |
| 114 |
|
| 115 |
__END__ |
| 116 |
|
| 117 |
=pod |
| 118 |
|
| 119 |
=encoding UTF-8 |
| 120 |
|
| 121 |
=head1 NAME |
| 122 |
|
| 123 |
HTML::FormHandler::Widget::Field::RadioGroup - radio group rendering widget |
| 124 |
|
| 125 |
=head1 VERSION |
| 126 |
|
| 127 |
version 0.410002 |
| 128 |
|
| 129 |
=head1 SYNOPSIS |
| 130 |
|
| 131 |
Renders a radio group (from a 'Select' field); |
| 132 |
|
| 133 |
Tags: radio_br_after |
| 134 |
|
| 135 |
See L<HTML::FormHandler::Field::Select> for documentation on |
| 136 |
select fields and options. |
| 137 |
|
| 138 |
=head1 AUTHOR |
| 139 |
|
| 140 |
FormHandler Contributors - see HTML::FormHandler |
| 141 |
|
| 142 |
=head1 COPYRIGHT AND LICENSE |
| 143 |
|
| 144 |
This software is copyright (c) 2017 by Gerda Shank. |
| 145 |
|
| 146 |
This is free software; you can redistribute it and/or modify it under |
| 147 |
the same terms as the Perl 5 programming language system itself. |
| 148 |
|
| 149 |
=cut |