File Coverage

blib/lib/Rose/HTML/Form/Field/PopUpMenu.pm
Criterion Covered Total %
statement 10 10 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod 2 2 100.0
total 16 16 100.0


line stmt bran cond sub pod time code
1              
2             use strict;
3 8     8   986  
  8         16  
  8         237  
4             use base 'Rose::HTML::Form::Field::SelectBox';
5 8     8   36  
  8         16  
  8         2581  
6             our $VERSION = '0.606';
7              
8             __PACKAGE__->required_html_attr_value(size => 1);
9             __PACKAGE__->delete_valid_html_attr('multiple');
10              
11              
12 623     623 1 1288 {
13             my($self) = shift;
14             my($value) = $self->SUPER::internal_value(@_);
15             return $value;
16 622     622 1 885 }
17 622         1386  
18 622         1548 1;
19              
20              
21             =head1 NAME
22              
23             Rose::HTML::Form::Field::PopUpMenu - Object representation of a pop-up menu in an HTML form.
24              
25             =head1 SYNOPSIS
26              
27             $field = Rose::HTML::Form::Field::PopUpMenu->new(name => 'fruits');
28              
29             $field->options(apple => 'Apple',
30             orange => 'Orange',
31             grape => 'Grape');
32              
33             print $field->value_label('apple'); # 'Apple'
34              
35             $field->input_value('orange');
36              
37             print $field->internal_value; # 'orange'
38              
39             print $field->html;
40              
41             ...
42              
43             =head1 DESCRIPTION
44              
45             L<Rose::HTML::Form::Field::PopUpMenu> is an object representation of a pop-up menu field in an HTML form.
46              
47             This class inherits from, and follows the conventions of, L<Rose::HTML::Form::Field>. Inherited methods that are not overridden will not be documented a second time here. See the L<Rose::HTML::Form::Field> documentation for more information.
48              
49             =head1 HIERARCHY
50              
51             All L<child-related|Rose::HTML::Object/HIERARCHY> methods are effectively aliases for the option manipulation methods described below. See the "hierarchy" sections of the L<Rose::HTML::Form::Field/HIERARCHY> and L<Rose::HTML::Form/HIERARCHY> documentation for an overview of the relationship between field and form objects and the child-related methods inherited from L<Rose::HTML::Object>.
52              
53             =head1 HTML ATTRIBUTES
54              
55             Valid attributes:
56              
57             accesskey
58             class
59             dir
60             disabled
61             id
62             lang
63             name
64             onblur
65             onchange
66             onclick
67             ondblclick
68             onfocus
69             onkeydown
70             onkeypress
71             onkeyup
72             onmousedown
73             onmousemove
74             onmouseout
75             onmouseover
76             onmouseup
77             size
78             style
79             tabindex
80             title
81             value
82             xml:lang
83              
84             Required attributes:
85              
86             name
87             size
88              
89             Boolean attributes:
90              
91             disabled
92              
93             =head1 CONSTRUCTOR
94              
95             =over 4
96              
97             =item B<new PARAMS>
98              
99             Constructs a new L<Rose::HTML::Form::Field::PopUpMenu> object based on PARAMS, where PARAMS are name/value pairs. Any object method is a valid parameter name.
100              
101             =back
102              
103             =head1 OBJECT METHODS
104              
105             =over 4
106              
107             =item B<add_option OPTION>
108              
109             Convenience alias for L<add_options()|/add_options>.
110              
111             =item B<add_options OPTIONS>
112              
113             Adds options to the pop-up menu. OPTIONS may take the following forms.
114              
115             A reference to a hash of value/label pairs:
116              
117             $field->add_options
118             (
119             {
120             value1 => 'label1',
121             value2 => 'label2',
122             ...
123             }
124             );
125              
126             An ordered list of value/label pairs:
127              
128             $field->add_options
129             (
130             value1 => 'label1',
131             value2 => 'label2',
132             ...
133             );
134              
135             (Option values and labels passed as a hash reference are sorted by the keys of the hash according to the default behavior of Perl's built-in L<sort()|perlfunc/sort> function.)
136              
137             A reference to an array of containing B<only> plain scalar values:
138              
139             $field->add_options([ 'value1', 'value2', ... ]);
140              
141             A list or reference to an array of L<Rose::HTML::Form::Field::Option> or L<Rose::HTML::Form::Field::OptionGroup> objects:
142              
143             $field->add_options
144             (
145             Rose::HTML::Form::Field::Option->new(...),
146             Rose::HTML::Form::Field::OptionGroup->new(...),
147             Rose::HTML::Form::Field::Option->new(...),
148             ...
149             );
150              
151             $field->add_options
152             (
153             [
154             Rose::HTML::Form::Field::Option->new(...),
155             Rose::HTML::Form::Field::OptionGroup->new(...),
156             Rose::HTML::Form::Field::Option->new(...),
157             ...
158             ]
159             );
160              
161             A list or reference to an array containing a mix of value/label pairs, value/hashref pairs, and L<Rose::HTML::Form::Field::Option> or L<Rose::HTML::Form::Field::OptionGroup> objects:
162              
163             @args =
164             (
165             # value/label pair
166             value1 => 'label1',
167              
168             # option group object
169             Rose::HTML::Form::Field::OptionGroup->new(...),
170              
171             # value/hashref pair
172             value2 =>
173             {
174             label => 'Some Label',
175             id => 'my_id',
176             ...
177             },
178              
179             # option object
180             Rose::HTML::Form::Field::Option->new(...),
181              
182             ...
183             );
184              
185             $field->add_options(@args); # list
186             $field->add_options(\@args); # reference to an array
187              
188             B<Please note:> the second form (passing a reference to an array) requires that at least one item in the referenced array is not a plain scalar, lest it be confused with "a reference to an array of containing only plain scalar values."
189              
190             All options are added to the end of the existing list of options.
191              
192             Option groups may also be added by nesting another level of array references. For example, this:
193              
194             $field = Rose::HTML::Form::Field::PopUpMenu->new(name => 'fruits');
195              
196             $field->options(apple => 'Apple',
197             orange => 'Orange',
198             grape => 'Grape');
199              
200             $group = Rose::HTML::Form::Field::OptionGroup->new(label => 'Others');
201              
202             $group->options(juji => 'Juji',
203             peach => 'Peach');
204              
205             $field->add_options($group);
206              
207             is equivalent to this:
208              
209             $field =
210             Rose::HTML::Form::Field::PopUpMenu->new(
211             name => 'fruits',
212             options =>
213             [
214             apple => 'Apple',
215             orange => 'Orange',
216             grape => 'Grape',
217             Others =>
218             [
219             juji => { label => 'Juji' },
220             peach => { label => 'Peach' },
221             ],
222             ]);
223              
224             $field->add_options($group);
225              
226             =item B<choices [OPTIONS]>
227              
228             This is an alias for the L<options|/options> method.
229              
230             =item B<delete_items_html_attr NAME>
231              
232             This is an alias for the L<delete_options_html_attr|/delete_options_html_attr> method.
233              
234             =item B<delete_option VALUE>
235              
236             Deletes the first option (according to the order that they are returned from L<options()|/options>) whose "value" HTML attribute is VALUE. Returns the deleted option or undef if no such option exists.
237              
238             =item B<delete_options LIST>
239              
240             Repeatedly calls L<delete_option|/delete_option>, passing each value in LIST as an arugment.
241              
242             =item B<delete_option_group LABEL>
243              
244             Deletes the first option group (according to the order that they are returned from L<options()|/options>) whose "label" HTML attribute is LABEL. Returns the deleted option group or undef if no such option exists.
245              
246             =item B<delete_option_groups LIST>
247              
248             Repeatedly calls L<delete_option_group|/delete_option_group>, passing each value in LIST.
249              
250             =item B<delete_options_html_attr NAME>
251              
252             Delete the L<HTML attribute|Rose::HTML::Object/html_attr> named NAME from each L<option|/options>.
253              
254             =item B<hide_all_options>
255              
256             Set L<hidden|Rose::HTML::Form::Field::Option/hidden> to true for all L<options|/options>.
257              
258             =item B<has_value VALUE>
259              
260             Returns true if VALUE is selected in the pop-up menu, false otherwise.
261              
262             =item B<items_html_attr NAME [, VALUE]>
263              
264             This is an alias for the L<options_html_attr|/options_html_attr> method.
265              
266             =item B<labels [LABELS]>
267              
268             Get or set the labels for all values. If LABELS is a reference to a hash or a list of value/label pairs, then LABELS replaces all existing labels. Passing an odd number of items in the list version of LABELS causes a fatal error.
269              
270             Returns a hash of value/label pairs in list context, or a reference to a hash in scalar context.
271              
272             =item B<label_ids [LABELS]>
273              
274             Get or set the integer L<message|Rose::HTML::Object::Messages> ids for all values. If LABELS is a reference to a hash or a list of value/message id pairs, then LABELS replaces all existing label ids.
275              
276             Returns a hash of value/label pairs in list context, or a reference to a hash of value/label pairs in scalar context.
277              
278             =item B<option VALUE>
279              
280             Returns the first option (according to the order that they are returned from L<options()|/options>) whose "value" HTML attribute is VALUE, or undef if no such option exists.
281              
282             =item B<options OPTIONS>
283              
284             Get or set the full list of options in the pop-up menu. OPTIONS may be a reference to a hash of value/label pairs, an ordered list of value/label pairs, a reference to an array of values, or a list of objects that are of, or inherit from, the classes L<Rose::HTML::Form::Field::Option> or L<Rose::HTML::Form::Field::OptionGroup>. Passing an odd number of items in the value/label argument list causes a fatal error. Options passed as a hash reference are sorted by value according to the default behavior of Perl's built-in L<sort()|perlfunc/sort> function.
285              
286             To set an ordered list of option values along with labels in the constructor, use both the L<options()|/options> and L<labels()|/labels> methods in the correct order. Example:
287              
288             $field =
289             Rose::HTML::Form::Field::PopUpMenu->new(
290             name => 'fruits',
291             options => [ 'apple', 'pear' ],
292             labels => { apple => 'Apple', pear => 'Pear' });
293              
294             Remember that methods are called in the order that they appear in the constructor arguments (see the L<Rose::Object> documentation), so L<options()|/options> will be called before L<labels()|/labels> in the example above. This is important; it will not work in the opposite order.
295              
296             Returns a list of the pop-up menu's L<Rose::HTML::Form::Field::Option> and/or L<Rose::HTML::Form::Field::OptionGroup> objects in list context, or a reference to an array of the same in scalar context. L<Hidden|Rose::HTML::Form::Field::Option/hidden> options I<will> be included in this list. These are the actual objects used in the field. Modifying them will modify the field itself.
297              
298             =item B<option_group LABEL>
299              
300             Returns the L<Rose::HTML::Form::Field::OptionGroup> object whose "label" HTML attribute is LABEL, or undef if no such option group exists.
301              
302             =item B<options_html_attr NAME [, VALUE]>
303              
304             If VALUE is passed, set the L<HTML attribute|Rose::HTML::Object/html_attr> named NAME on all L<options|/options>. Otherwise, return the value of the L<HTML attribute|Rose::HTML::Object/html_attr> named NAME on the first option encountered in the list of all L<options|/options>.
305              
306             =item B<show_all_options>
307              
308             Set L<hidden|Rose::HTML::Form::Field::Option/hidden> to false for all L<options|/options>.
309              
310             =item B<value [VALUE]>
311              
312             Simply calls L<input_value()|Rose::HTML::Form::Field/input_value>, passing all arguments.
313              
314             =item B<value_label [VALUE [, LABEL]]>
315              
316             If no arguments are passed, it returns the label of the selected option, or the value itself if it has no label. If no option is selected, undef is returned.
317              
318             With arguments, it will get or set the label for the option whose value is VALUE. The label for that option is returned. If the option exists, but has no label, then the value itself is returned. If the option does not exist, then undef is returned.
319              
320             =back
321              
322             =head1 AUTHOR
323              
324             John C. Siracusa (siracusa@gmail.com)
325              
326             =head1 LICENSE
327              
328             Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.