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