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             package Rose::HTML::Form::Field::PopUpMenu;
2              
3 8     8   1640 use strict;
  8         18  
  8         298  
4              
5 8     8   58 use base 'Rose::HTML::Form::Field::SelectBox';
  8         28  
  8         3378  
6              
7             our $VERSION = '0.606';
8              
9             __PACKAGE__->required_html_attr_value(size => 1);
10             __PACKAGE__->delete_valid_html_attr('multiple');
11              
12 626     626 1 1639 sub multiple { 0 }
13              
14             sub internal_value
15             {
16 625     625 1 1011 my($self) = shift;
17 625         1623 my($value) = $self->SUPER::internal_value(@_);
18 625         1871 return $value;
19             }
20              
21             1;
22              
23             __END__
24              
25             =head1 NAME
26              
27             Rose::HTML::Form::Field::PopUpMenu - Object representation of a pop-up menu in an HTML form.
28              
29             =head1 SYNOPSIS
30              
31             $field = Rose::HTML::Form::Field::PopUpMenu->new(name => 'fruits');
32              
33             $field->options(apple => 'Apple',
34             orange => 'Orange',
35             grape => 'Grape');
36              
37             print $field->value_label('apple'); # 'Apple'
38              
39             $field->input_value('orange');
40              
41             print $field->internal_value; # 'orange'
42              
43             print $field->html;
44              
45             ...
46              
47             =head1 DESCRIPTION
48              
49             L<Rose::HTML::Form::Field::PopUpMenu> is an object representation of a pop-up menu field in an HTML form.
50              
51             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.
52              
53             =head1 HIERARCHY
54              
55             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>.
56              
57             =head1 HTML ATTRIBUTES
58              
59             Valid attributes:
60              
61             accesskey
62             class
63             dir
64             disabled
65             id
66             lang
67             name
68             onblur
69             onchange
70             onclick
71             ondblclick
72             onfocus
73             onkeydown
74             onkeypress
75             onkeyup
76             onmousedown
77             onmousemove
78             onmouseout
79             onmouseover
80             onmouseup
81             size
82             style
83             tabindex
84             title
85             value
86             xml:lang
87              
88             Required attributes:
89              
90             name
91             size
92              
93             Boolean attributes:
94              
95             disabled
96              
97             =head1 CONSTRUCTOR
98              
99             =over 4
100              
101             =item B<new PARAMS>
102              
103             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.
104              
105             =back
106              
107             =head1 OBJECT METHODS
108              
109             =over 4
110              
111             =item B<add_option OPTION>
112              
113             Convenience alias for L<add_options()|/add_options>.
114              
115             =item B<add_options OPTIONS>
116              
117             Adds options to the pop-up menu. OPTIONS may take the following forms.
118              
119             A reference to a hash of value/label pairs:
120              
121             $field->add_options
122             (
123             {
124             value1 => 'label1',
125             value2 => 'label2',
126             ...
127             }
128             );
129              
130             An ordered list of value/label pairs:
131              
132             $field->add_options
133             (
134             value1 => 'label1',
135             value2 => 'label2',
136             ...
137             );
138              
139             (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.)
140              
141             A reference to an array of containing B<only> plain scalar values:
142              
143             $field->add_options([ 'value1', 'value2', ... ]);
144              
145             A list or reference to an array of L<Rose::HTML::Form::Field::Option> or L<Rose::HTML::Form::Field::OptionGroup> objects:
146              
147             $field->add_options
148             (
149             Rose::HTML::Form::Field::Option->new(...),
150             Rose::HTML::Form::Field::OptionGroup->new(...),
151             Rose::HTML::Form::Field::Option->new(...),
152             ...
153             );
154              
155             $field->add_options
156             (
157             [
158             Rose::HTML::Form::Field::Option->new(...),
159             Rose::HTML::Form::Field::OptionGroup->new(...),
160             Rose::HTML::Form::Field::Option->new(...),
161             ...
162             ]
163             );
164              
165             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:
166              
167             @args =
168             (
169             # value/label pair
170             value1 => 'label1',
171              
172             # option group object
173             Rose::HTML::Form::Field::OptionGroup->new(...),
174              
175             # value/hashref pair
176             value2 =>
177             {
178             label => 'Some Label',
179             id => 'my_id',
180             ...
181             },
182              
183             # option object
184             Rose::HTML::Form::Field::Option->new(...),
185              
186             ...
187             );
188              
189             $field->add_options(@args); # list
190             $field->add_options(\@args); # reference to an array
191              
192             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."
193              
194             All options are added to the end of the existing list of options.
195              
196             Option groups may also be added by nesting another level of array references. For example, this:
197              
198             $field = Rose::HTML::Form::Field::PopUpMenu->new(name => 'fruits');
199              
200             $field->options(apple => 'Apple',
201             orange => 'Orange',
202             grape => 'Grape');
203              
204             $group = Rose::HTML::Form::Field::OptionGroup->new(label => 'Others');
205              
206             $group->options(juji => 'Juji',
207             peach => 'Peach');
208              
209             $field->add_options($group);
210              
211             is equivalent to this:
212              
213             $field =
214             Rose::HTML::Form::Field::PopUpMenu->new(
215             name => 'fruits',
216             options =>
217             [
218             apple => 'Apple',
219             orange => 'Orange',
220             grape => 'Grape',
221             Others =>
222             [
223             juji => { label => 'Juji' },
224             peach => { label => 'Peach' },
225             ],
226             ]);
227              
228             $field->add_options($group);
229              
230             =item B<choices [OPTIONS]>
231              
232             This is an alias for the L<options|/options> method.
233              
234             =item B<delete_items_html_attr NAME>
235              
236             This is an alias for the L<delete_options_html_attr|/delete_options_html_attr> method.
237              
238             =item B<delete_option VALUE>
239              
240             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.
241              
242             =item B<delete_options LIST>
243              
244             Repeatedly calls L<delete_option|/delete_option>, passing each value in LIST as an arugment.
245              
246             =item B<delete_option_group LABEL>
247              
248             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.
249              
250             =item B<delete_option_groups LIST>
251              
252             Repeatedly calls L<delete_option_group|/delete_option_group>, passing each value in LIST.
253              
254             =item B<delete_options_html_attr NAME>
255              
256             Delete the L<HTML attribute|Rose::HTML::Object/html_attr> named NAME from each L<option|/options>.
257              
258             =item B<hide_all_options>
259              
260             Set L<hidden|Rose::HTML::Form::Field::Option/hidden> to true for all L<options|/options>.
261              
262             =item B<has_value VALUE>
263              
264             Returns true if VALUE is selected in the pop-up menu, false otherwise.
265              
266             =item B<items_html_attr NAME [, VALUE]>
267              
268             This is an alias for the L<options_html_attr|/options_html_attr> method.
269              
270             =item B<labels [LABELS]>
271              
272             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.
273              
274             Returns a hash of value/label pairs in list context, or a reference to a hash in scalar context.
275              
276             =item B<label_ids [LABELS]>
277              
278             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.
279              
280             Returns a hash of value/label pairs in list context, or a reference to a hash of value/label pairs in scalar context.
281              
282             =item B<option VALUE>
283              
284             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.
285              
286             =item B<options OPTIONS>
287              
288             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.
289              
290             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:
291              
292             $field =
293             Rose::HTML::Form::Field::PopUpMenu->new(
294             name => 'fruits',
295             options => [ 'apple', 'pear' ],
296             labels => { apple => 'Apple', pear => 'Pear' });
297              
298             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.
299              
300             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.
301              
302             =item B<option_group LABEL>
303              
304             Returns the L<Rose::HTML::Form::Field::OptionGroup> object whose "label" HTML attribute is LABEL, or undef if no such option group exists.
305              
306             =item B<options_html_attr NAME [, VALUE]>
307              
308             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>.
309              
310             =item B<show_all_options>
311              
312             Set L<hidden|Rose::HTML::Form::Field::Option/hidden> to false for all L<options|/options>.
313              
314             =item B<value [VALUE]>
315              
316             Simply calls L<input_value()|Rose::HTML::Form::Field/input_value>, passing all arguments.
317              
318             =item B<value_label [VALUE [, LABEL]]>
319              
320             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.
321              
322             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.
323              
324             =back
325              
326             =head1 AUTHOR
327              
328             John C. Siracusa (siracusa@gmail.com)
329              
330             =head1 LICENSE
331              
332             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.