Debian Codegraph @

lib/HTML/FormHandler/Widget/Field/Select.pm

raw
1 package HTML::FormHandler::Widget::Field::Select;
2 # ABSTRACT: select field rendering widget
3 $HTML::FormHandler::Widget::Field::Select::VERSION = '0.410002';
4
5 use Moose::Role;
6 use HTML::Entities qw( encode_entities );
7 use namespace::autoclean;
8 use HTML::FormHandler::Render::Util ('process_attrs');
9
10 sub render {
11 my ( $self, $result ) = @_;
12 $result ||= $self->result;
13 die "No result for form field '" . $self->full_name . "'. Field may be inactive." unless $result;
14 my $output = $self->render_element( $result );
15 return $self->wrap_field( $result, $output );
16 }
17
18 sub render_element {
19 my ( $self, $result ) = @_;
20 $result ||= $self->result;
21
22 # create select element
23 my $output = $self->render_select_start( $result );
24
25 # create empty select
26 if( defined $self->empty_select ) {
27 $output .= $self->render_empty_select;
28 }
29
30 # loop through options
31 foreach my $option ( @{ $self->{options} } ) {
32 if ( my $label = $option->{group} ) {
33 $label = $self->_localize( $label ) if $self->localize_labels;
34 $output .= qq{\n<optgroup label="} . encode_entities($label, '"&') . qq{">};
35 foreach my $group_opt ( @{ $option->{options} } ) {
36 $output .= $self->render_option( $group_opt, $result );
37 }
38 $output .= qq{\n</optgroup>};
39 }
40 else {
41 $output .= $self->render_option( $option, $result );
42 }
43 }
44 $self->reset_options_index;
45
46 $output .= '</select>';
47 return $output;
48 }
49
50 sub render_select_start {
51 my ( $self, $result ) = @_;
52 $result ||= $self->result;
53
54 my $id = $self->id;
55 my $output = '<select name="' . $self->html_name . qq{" id="$id"};
56 $output .= ' multiple="multiple"' if $self->multiple;
57 $output .= ' size="' . $self->size . '"' if defined $self->size;
58 $output .= process_attrs($self->element_attributes($result));
59 $output .= '>';
60 return $output;
61 }
62
63 sub render_empty_select {
64 my $self = shift;
65
66 my $label = $self->_localize($self->empty_select);
67 my $id = $self->id . "." . $self->options_index;
68 my $output = qq{\n<option value="" id="$id">$label</option>};
69 $self->inc_options_index;
70 return $output;
71 }
72
73 sub render_option {
74 my ( $self, $option, $result ) = @_;
75 $result ||= $self->result;
76
77 # current values
78 my $fif = $result->fif;
79 my %fif_lookup;
80 @fif_lookup{@$fif} = () if $self->multiple;
81
82 # not sure why the value of an option would be undef, but just in case,
83 # set to empty string because of 'eq' below
84 my $value = defined $option->{value} ? $option->{value} : '';
85 my $id = $self->id . '.' . $self->options_index;
86 my $output = qq{\n<option value="} . $self->html_filter($value) . '"';
87 $output .= qq{ id="$id"};
88
89 # handle option attributes
90 my $attrs = $option->{attributes} || {};
91 if( defined $option->{disabled} && $option->{disabled} ) {
92 $attrs->{disabled} = 'disabled';
93 }
94 if ( defined $fif &&
95 ( ( $self->multiple && exists $fif_lookup{$value} ) ||
96 ( $fif eq $value ) ) ) {
97 $attrs->{selected} = 'selected';
98 }
99 $output .= process_attrs($attrs);
100
101 # handle label
102 my $label = $option->{label};
103 $label = $self->_localize($label) if $self->localize_labels;
104 $output .= '>' . encode_entities($label, '<>&') . '</option>';
105 $self->inc_options_index;
106 return $output;
107 }
108
109 1;
110
111 __END__
112
113 =pod
114
115 =encoding UTF-8
116
117 =head1 NAME
118
119 HTML::FormHandler::Widget::Field::Select - select field rendering widget
120
121 =head1 VERSION
122
123 version 0.410002
124
125 =head1 DESCRIPTION
126
127 Renders single and multiple selects. Options hashrefs must
128 have 'value' and 'label' keys, and may have an 'attributes' key.
129
130 See L<HTML::FormHandler::Field::Select> for documentation on
131 select fields and options.
132
133 =head1 NAME
134
135 HTML::FormHandler::Widget::Field::Select
136
137 =head1 AUTHOR
138
139 FormHandler Contributors - see HTML::FormHandler
140
141 =head1 COPYRIGHT AND LICENSE
142
143 This software is copyright (c) 2017 by Gerda Shank.
144
145 This is free software; you can redistribute it and/or modify it under
146 the same terms as the Perl 5 programming language system itself.
147
148 =cut