File Coverage

blib/lib/Rose/HTML/Form/Field/SelectBox.pm
Criterion Covered Total %
statement 16 17 94.1
branch 2 2 100.0
condition n/a
subroutine 7 8 87.5
pod 5 5 100.0
total 30 32 93.7


line stmt bran cond sub pod time code
1             package Rose::HTML::Form::Field::SelectBox;
2              
3 10     10   5294 use strict;
  10         22  
  10         296  
4              
5 10     10   48 use Carp();
  10         18  
  10         178  
6              
7 10     10   46 use base 'Rose::HTML::Form::Field::Option::Container';
  10         18  
  10         4850  
8              
9             our $VERSION = '0.606';
10              
11             __PACKAGE__->add_required_html_attrs(
12             {
13             name => '',
14             size => 5,
15             });
16              
17             __PACKAGE__->add_boolean_html_attrs
18             (
19             'multiple',
20             'disabled',
21             );
22              
23             __PACKAGE__->add_valid_html_attrs
24             (
25             'onchange', # %Script; #IMPLIED -- the element value was changed --
26             );
27              
28             *options_html_attr = \&Rose::HTML::Form::Field::Group::items_html_attr;
29             *delete_options_html_attr = \&Rose::HTML::Form::Field::Group::delete_items_html_attr;
30              
31 0     0 1 0 sub element { 'select' }
32 144     144 1 1124 sub html_element { 'select' }
33 54     54 1 370 sub xhtml_element { 'select' }
34              
35 55     55 1 164 sub multiple { shift->html_attr('multiple', @_) }
36              
37             sub internal_value
38             {
39 652     652 1 1110 my($self) = shift;
40              
41 652 100       1419 return $self->SUPER::internal_value(@_) if($self->multiple);
42              
43 631         1886 my($value) = $self->SUPER::internal_value(@_);
44 631         1547 return $value;
45             }
46              
47             1;
48              
49             __END__
50              
51             =head1 NAME
52              
53             Rose::HTML::Form::Field::SelectBox - Object representation of a select box in an HTML form.
54              
55             =head1 SYNOPSIS
56              
57             $field = Rose::HTML::Form::Field::SelectBox->new(name => 'fruits');
58              
59             $field->options(apple => 'Apple',
60             orange => 'Orange',
61             grape => 'Grape');
62              
63             print $field->value_label('apple'); # 'Apple'
64              
65             $field->input_value('orange');
66             print $field->internal_value; # 'orange'
67              
68             $field->multiple(1);
69             $field->add_value('grape');
70             print join(',', $field->internal_value); # 'grape,orange'
71              
72             $field->has_value('grape'); # true
73             $field->has_value('apple'); # false
74              
75             print $field->html;
76              
77             ...
78              
79             =head1 DESCRIPTION
80              
81             L<Rose::HTML::Form::Field::SelectBox> is an object representation of a select box field in an HTML form.
82              
83             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.
84              
85             =head1 HIERARCHY
86              
87             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>.
88              
89             =head1 HTML ATTRIBUTES
90              
91             Valid attributes:
92              
93             accesskey
94             class
95             dir
96             disabled
97             id
98             lang
99             multiple
100             name
101             onblur
102             onchange
103             onclick
104             ondblclick
105             onfocus
106             onkeydown
107             onkeypress
108             onkeyup
109             onmousedown
110             onmousemove
111             onmouseout
112             onmouseover
113             onmouseup
114             size
115             style
116             tabindex
117             title
118             value
119             xml:lang
120              
121             Required attributes:
122              
123             name
124             size
125              
126             Boolean attributes:
127              
128             disabled
129             multiple
130              
131             =head1 CONSTRUCTOR
132              
133             =over 4
134              
135             =item B<new PARAMS>
136              
137             Constructs a new L<Rose::HTML::Form::Field::SelectBox> object based on PARAMS, where PARAMS are name/value pairs. Any object method is a valid parameter name.
138              
139             =back
140              
141             =head1 OBJECT METHODS
142              
143             =over 4
144              
145             =item B<add_option OPTION>
146              
147             Convenience alias for L<add_options()|/add_options>.
148              
149             =item B<add_options OPTIONS>
150              
151             Adds options to the select box. OPTIONS may take the following forms.
152              
153             A reference to a hash of value/label pairs:
154              
155             $field->add_options
156             (
157             {
158             value1 => 'label1',
159             value2 => 'label2',
160             ...
161             }
162             );
163              
164             An ordered list of value/label pairs:
165              
166             $field->add_options
167             (
168             value1 => 'label1',
169             value2 => 'label2',
170             ...
171             );
172              
173             (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.)
174              
175             A reference to an array of containing B<only> plain scalar values:
176              
177             $field->add_options([ 'value1', 'value2', ... ]);
178              
179             A list or reference to an array of L<Rose::HTML::Form::Field::Option> or L<Rose::HTML::Form::Field::OptionGroup> objects:
180              
181             $field->add_options
182             (
183             Rose::HTML::Form::Field::Option->new(...),
184             Rose::HTML::Form::Field::OptionGroup->new(...),
185             Rose::HTML::Form::Field::Option->new(...),
186             ...
187             );
188              
189             $field->add_options
190             (
191             [
192             Rose::HTML::Form::Field::Option->new(...),
193             Rose::HTML::Form::Field::OptionGroup->new(...),
194             Rose::HTML::Form::Field::Option->new(...),
195             ...
196             ]
197             );
198              
199             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:
200              
201             @args =
202             (
203             # value/label pair
204             value1 => 'label1',
205              
206             # option group object
207             Rose::HTML::Form::Field::OptionGroup->new(...),
208              
209             # value/hashref pair
210             value2 =>
211             {
212             label => 'Some Label',
213             id => 'my_id',
214             ...
215             },
216              
217             # option object
218             Rose::HTML::Form::Field::Option->new(...),
219              
220             ...
221             );
222              
223             $field->add_options(@args); # list
224             $field->add_options(\@args); # reference to an array
225              
226             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."
227              
228             All options are added to the end of the existing list of options.
229              
230             Option groups may also be added by nesting another level of array references. For example, this:
231              
232             $field = Rose::HTML::Form::Field::SelectBox->new(name => 'fruits');
233              
234             $field->options(apple => 'Apple',
235             orange => 'Orange',
236             grape => 'Grape');
237              
238             $group = Rose::HTML::Form::Field::OptionGroup->new(label => 'Others');
239              
240             $group->options(juji => 'Juji',
241             peach => 'Peach');
242              
243             $field->add_options($group);
244              
245             is equivalent to this:
246              
247             $field =
248             Rose::HTML::Form::Field::SelectBox->new(
249             name => 'fruits',
250             options =>
251             [
252             apple => 'Apple',
253             orange => 'Orange',
254             grape => 'Grape',
255             Others =>
256             [
257             juji => { label => 'Juji' },
258             peach => { label => 'Peach' },
259             ],
260             ]);
261              
262             $field->add_options($group);
263              
264             =item B<add_value VALUE>
265              
266             Add VALUE to the list of selected values.
267              
268             =item B<add_values VALUE1, VALUE2, ...>
269              
270             Add multiple values to the list of selected values.
271              
272             =item B<choices [OPTIONS]>
273              
274             This is an alias for the L<options|/options> method.
275              
276             =item B<delete_items_html_attr NAME>
277              
278             This is an alias for the L<delete_options_html_attr|/delete_options_html_attr> method.
279              
280             =item B<delete_option VALUE>
281              
282             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.
283              
284             =item B<delete_options LIST>
285              
286             Repeatedly calls L<delete_option|/delete_option>, passing each value in LIST as an arugment.
287              
288             =item B<delete_option_group LABEL>
289              
290             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.
291              
292             =item B<delete_option_groups LIST>
293              
294             Repeatedly calls L<delete_option_group|/delete_option_group>, passing each value in LIST.
295              
296             =item B<delete_options_html_attr NAME>
297              
298             Delete the L<HTML attribute|Rose::HTML::Object/html_attr> named NAME from each L<option|/options>.
299              
300             =item B<has_value VALUE>
301              
302             Returns true if VALUE is selected in the select box, false otherwise.
303              
304             =item B<hide_all_options>
305              
306             Set L<hidden|Rose::HTML::Form::Field::Option/hidden> to true for all L<options|/options>.
307              
308             =item B<items_html_attr NAME [, VALUE]>
309              
310             This is an alias for the L<options_html_attr|/options_html_attr> method.
311              
312             =item B<internal_value>
313              
314             If L<multiple|/multiple> is true, a reference to an array of selected values is returned in scalar context, and a list of selected values is returned in list context. Otherwise, the selected value is returned (or undef if no value is selected).
315              
316             =item B<labels [LABELS]>
317              
318             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.
319              
320             Returns a hash of value/label pairs in list context, or a reference to a hash of value/label pairs in scalar context.
321              
322             =item B<label_ids [LABELS]>
323              
324             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.
325              
326             Returns a hash of value/label pairs in list context, or a reference to a hash of value/label pairs in scalar context.
327              
328             =item B<multiple [BOOL]>
329              
330             This is just an accessor method for the "multiple" boolean HTML attribute, but I'm documenting it here so that I can warn that trying to select multiple values in a non-multiple-valued select box will cause a fatal error.
331              
332             =item B<option VALUE>
333              
334             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.
335              
336             =item B<options [OPTIONS]>
337              
338             Get or set the full list of options in the select box. 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.
339              
340             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:
341              
342             $field =
343             Rose::HTML::Form::Field::SelectBox->new(
344             name => 'fruits',
345             options => [ 'apple', 'pear' ],
346             labels => { apple => 'Apple', pear => 'Pear' });
347              
348             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.
349              
350             Returns a list of the select box'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.
351              
352             =item B<option_group LABEL>
353              
354             Returns the L<Rose::HTML::Form::Field::OptionGroup> object whose "label" HTML attribute is LABEL, or undef if no such option group exists.
355              
356             =item B<options_html_attr NAME [, VALUE]>
357              
358             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>.
359              
360             =item B<show_all_options>
361              
362             Set L<hidden|Rose::HTML::Form::Field::Option/hidden> to false for all L<options|/options>.
363              
364             =item B<value [VALUE]>
365              
366             Simply calls L<input_value()|Rose::HTML::Form::Field/input_value>, passing all arguments.
367              
368             =item B<values [VALUE]>
369              
370             Simply calls L<input_value()|Rose::HTML::Form::Field/input_value>, passing all arguments.
371              
372             =item B<value_label>
373              
374             Returns the label of the first selected value (according to the order that they are returned by L<internal_value()|Rose::HTML::Form::Field/internal_value>), or the value itself if it has no label. If no value is selected, undef is returned.
375              
376             =item B<value_labels>
377              
378             Returns an array (in list context) or reference to an array (in scalar context) of the labels of the selected values. If a value has no label, the value itself is substituted. If no values are selected, then an empty array (in list context) or reference to an empty array (in scalar context) is returned.
379              
380             =back
381              
382             =head1 AUTHOR
383              
384             John C. Siracusa (siracusa@gmail.com)
385              
386             =head1 LICENSE
387              
388             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.