File Coverage

blib/lib/Rose/HTML/Form/Field/CheckboxGroup.pm
Criterion Covered Total %
statement 25 27 92.5
branch 7 8 87.5
condition 2 4 50.0
subroutine 8 10 80.0
pod 2 2 100.0
total 44 51 86.2


line stmt bran cond sub pod time code
1              
2             use strict;
3 5     5   9165  
  5         10  
  5         130  
4             use Carp;
5 5     5   21  
  5         8  
  5         262  
6             use base 'Rose::HTML::Form::Field::Group::OnOff';
7 5     5   25  
  5         8  
  5         1304  
8             use Rose::HTML::Form::Field::Group;
9 5     5   31 use Rose::HTML::Form::Field::Checkbox;
  5         11  
  5         36  
10 5     5   1015  
  5         12  
  5         31  
11             our $VERSION = '0.606';
12              
13              
14 14     14   53 *checkboxes = \&Rose::HTML::Form::Field::Group::items;
15 0     0   0 *visible_checkboxes = \&Rose::HTML::Form::Field::Group::visible_items;
16 0     0   0  
17             *checkbox = \&Rose::HTML::Form::Field::Group::OnOff::item;
18             *add_checkboxes = \&Rose::HTML::Form::Field::Group::add_items;
19             *add_checkbox = \&add_checkboxes;
20              
21             *choices = \&checkboxes;
22              
23             *show_all_checkboxes = \&Rose::HTML::Form::Field::Group::show_all_items;
24             *hide_all_checkboxes = \&Rose::HTML::Form::Field::Group::hide_all_items;
25              
26             *delete_checkbox = \&Rose::HTML::Form::Field::Group::delete_item;
27             *delete_checkboxes = \&Rose::HTML::Form::Field::Group::delete_items;
28              
29             *delete_checkbox_group = \&Rose::HTML::Form::Field::Group::delete_item_group;
30             *delete_checkboxes_groups = \&Rose::HTML::Form::Field::Group::delete_item_groups;
31              
32             *checkboxes_html_attr = \&Rose::HTML::Form::Field::Group::items_html_attr;
33             *delete_checkboxes_html_attr = \&Rose::HTML::Form::Field::Group::delete_items_html_attr;
34              
35             {
36             my($self, %args) = @_;
37              
38             $args{'class'} = defined $args{'class'} ?
39             "$args{'class'} checkbox-group" : 'checkbox-group';
40              
41 4     4 1 16 #$args{'cellpadding'} = 2 unless(exists $args{'cellpadding'});
42             #$args{'cellspacing'} = 0 unless(exists $args{'cellspacing'});
43 4 100       19 #$args{'tr'} = { valign => 'top' } unless(exists $args{'tr'});
44              
45             $args{'tr'} ||= {};
46             $args{'td'} ||= {};
47              
48             $args{'table'}{'class'} = defined $args{'table'}{'class'} ?
49             "$args{'table'}{'class'} checkbox-group" :
50 4   50     22 defined $args{'class'} ? $args{'class'} : undef;
51 4   50     23  
52             #$args{'table'}{'cellpadding'} = $args{'cellpadding'}
53             # unless((exists $args{'table'} && !defined $args{'table'}) ||
54             # exists $args{'table'}{'cellpadding'});
55 4 50       21  
    100          
56             #$args{'table'}{'cellspacing'} = $args{'cellspacing'}
57             # unless((exists $args{'table'} && !defined $args{'table'}) ||
58             # exists $args{'table'}{'cellspacing'});
59              
60             if($args{'_xhtml'})
61             {
62             return
63             $self->SUPER::html_table(items => scalar $self->visible_checkboxes,
64             format_item => \&Rose::HTML::Form::Field::Group::_xhtml_item,
65 4 100       8 %args);
66             }
67             else
68 1         5 {
69             return
70             $self->SUPER::html_table(items => scalar $self->visible_checkboxes,
71             format_item => \&Rose::HTML::Form::Field::Group::_html_item,
72             %args);
73             }
74             }
75 3         14  
76              
77             1;
78              
79              
80             =head1 NAME
81 1     1 1 6  
82             Rose::HTML::Form::Field::CheckboxGroup - A group of checkboxes in an HTML form.
83              
84             =head1 SYNOPSIS
85              
86             $field =
87             Rose::HTML::Form::Field::CheckboxGroup->new(name => 'fruits');
88              
89             $field->checkboxes(apple => 'Apple',
90             orange => 'Orange',
91             grape => 'Grape');
92              
93             print $field->value_label('apple'); # 'Apple'
94              
95             $field->input_value('orange');
96             print $field->internal_value; # 'orange'
97              
98             $field->add_value('grape');
99             print join(',', $field->internal_value); # 'grape,orange'
100              
101             $field->has_value('grape'); # true
102             $field->has_value('apple'); # false
103              
104             print $field->html_table(columns => 2);
105              
106             ...
107              
108             =head1 DESCRIPTION
109              
110             L<Rose::HTML::Form::Field::CheckboxGroup> is an object wrapper for a group of checkboxes in an HTML form.
111              
112             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.
113              
114             =head1 HIERARCHY
115              
116             A checkbox group is an abstraction with no corresponding parent HTML element; the individual L<checkbox|Rose::HTML::Form::Field::Checkbox> objects in the group exist as siblings. As such, the list of L<child|Rose::HTML::Object/HIERARCHY> objects will always be empty and cannot be modified. To get the list of siblings, use the L<checkboxes|/checkboxes> method.
117              
118             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>.
119              
120             =head1 HTML ATTRIBUTES
121              
122             None. This class is simply an aggregator of L<Rose::HTML::Form::Field::Checkbox> objects.
123              
124             =head1 CONSTRUCTOR
125              
126             =over 4
127              
128             =item B<new PARAMS>
129              
130             Constructs a new L<Rose::HTML::Form::Field::CheckboxGroup> 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_checkbox OPTION>
139              
140             Convenience alias for L<add_checkboxes()|/add_checkboxes>.
141              
142             =item B<add_checkboxes CHECKBOXES>
143              
144             Adds checkboxes to the checkbox group. CHECKBOXES may take the following forms.
145              
146             A reference to a hash of value/label pairs:
147              
148             $field->add_checkboxes
149             (
150             {
151             value1 => 'label1',
152             value2 => 'label2',
153             ...
154             }
155             );
156              
157             An ordered list of value/label pairs:
158              
159             $field->add_checkboxes
160             (
161             value1 => 'label1',
162             value2 => 'label2',
163             ...
164             );
165              
166             (Checkbox values and labels passed as a hash reference are sorted by value 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_checkboxes([ 'value1', 'value2', ... ]);
171              
172             A list or reference to an array of L<Rose::HTML::Form::Field::Checkbox> objects:
173              
174             $field->add_checkboxes
175             (
176             Rose::HTML::Form::Field::Checkbox->new(...),
177             Rose::HTML::Form::Field::Checkbox->new(...),
178             ...
179             );
180              
181             $field->add_checkboxes
182             (
183             [
184             Rose::HTML::Form::Field::Checkbox->new(...),
185             Rose::HTML::Form::Field::Checkbox->new(...),
186             ...
187             ]
188             );
189              
190             A list or reference to an array containing a mix of value/label pairs, value/hashref pairs, and L<Rose::HTML::Form::Field::Checkbox> objects:
191              
192             @args =
193             (
194             # value/label pair
195             value1 => 'label1',
196              
197             # value/hashref pair
198             value2 =>
199             {
200             label => 'Some Label',
201             id => 'my_id',
202             ...
203             },
204              
205             # object
206             Rose::HTML::Form::Field::Checkbox->new(...),
207              
208             ...
209             );
210              
211             $field->add_checkboxes(@args); # list
212             $field->add_checkboxes(\@args); # reference to an array
213              
214             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."
215              
216             All checkboxes are added to the end of the existing list of checkboxes.
217              
218             =item B<add_value VALUE>
219              
220             Put a check in the checkbox whose value is VALUE.
221              
222             =item B<add_values VALUE1, VALUE2, ...>
223              
224             Put a check in the checkboxes whose values are equal to VALUE1, VALUE2, etc.
225              
226             =item B<checkbox VALUE>
227              
228             Returns the first checkbox (according to the order that they are returned from L<checkboxes()|/checkboxes>) whose "value" HTML attribute is VALUE, or undef if no such checkbox exists.
229              
230             =item B<checkboxes [CHECKBOXES]>
231              
232             Get or set the full list of checkboxes in the group. CHECKBOXES 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 L<Rose::HTML::Form::Field::Checkbox> objects. Passing an odd number of items in the value/label argument list causes a fatal error. Checkbox values and labels passed as a hash reference are sorted by value according to the default behavior of Perl's built-in L<sort()|perlfunc/sort> function.
233              
234             To set an ordered list of checkboxes along with labels in the constructor, use both the L<checkboxes()|/checkboxes> and L<labels()|/labels> methods in the correct order. Example:
235              
236             $field =
237             Rose::HTML::Form::Field::CheckboxGroup->new(
238             name => 'fruits',
239             checkboxes => [ 'apple', 'pear' ],
240             labels => { apple => 'Apple', pear => 'Pear' });
241              
242             Remember that methods are called in the order that they appear in the constructor arguments (see the L<Rose::Object> documentation), so L<checkboxes()|/checkboxes> will be called before L<labels()|/labels> in the example above. This is important; it will not work in the opposite order.
243              
244             Returns a list of the checkbox group's L<Rose::HTML::Form::Field::Checkbox> objects in list context, or a reference to an array of the same in scalar context. L<Hidden|Rose::HTML::Form::Field::Checkbox/hidden> checkboxes I<will> be included in this list. These are the actual objects used in the field. Modifying them will modify the field itself.
245              
246             =item B<choices [CHECKBOXES]>
247              
248             This is an alias for the L<checkboxes|/checkboxes> method.
249              
250             =item B<columns [COLS]>
251              
252             Get or set the default number of columns to use in the output of the L<html_table()|/html_table> and L<xhtml_table()|/xhtml_table> methods.
253              
254             =item B<checkboxes_html_attr NAME [, VALUE]>
255              
256             If VALUE is passed, set the L<HTML attribute|Rose::HTML::Object/html_attr> named NAME on all L<checkboxes|/checkboxes>. Otherwise, return the value of the L<HTML attribute|Rose::HTML::Object/html_attr> named NAME on the first checkbox encountered in the list of all L<checkboxes|/checkboxes>.
257              
258             =item B<delete_checkbox VALUE>
259              
260             Deletes the first checkbox (according to the order that they are returned from L<checkboxes()|/checkboxes>) whose "value" HTML attribute is VALUE. Returns the deleted checkbox or undef if no such checkbox exists.
261              
262             =item B<delete_checkboxes LIST>
263              
264             Repeatedly calls L<delete_checkbox|/delete_checkbox>, passing each value in LIST.
265              
266             Deletes the first checkbox (according to the order that they are returned from L<checkboxes()|/checkboxes>) whose "value" HTML attribute is VALUE, or undef if no such checkbox exists.
267              
268             =item B<delete_checkboxes_html_attr NAME>
269              
270             Delete the L<HTML attribute|Rose::HTML::Object/html_attr> named NAME from each L<checkbox|/checkboxes>.
271              
272             =item B<delete_items_html_attr NAME>
273              
274             This is an alias for the L<delete_checkboxes_html_attr|/delete_checkboxes_html_attr> method.
275              
276             =item B<has_value VALUE>
277              
278             Returns true if the checkbox whose value is VALUE is checked, false otherwise.
279              
280             =item B<hide_all_checkboxes>
281              
282             Set L<hidden|Rose::HTML::Form::Field::Checkbox/hidden> to true for all L<checkboxes|/checkboxes>.
283              
284             =item B<html>
285              
286             Returns the HTML for checkbox group, which consists of the L<html()|/html> for each checkbox object joined by L<html_linebreak()|/html_linebreak> if L<linebreak()|/linebreak> is true, or single spaces if it is false.
287              
288             =item B<html_linebreak [HTML]>
289              
290             Get or set the HTML linebreak string. The default is "E<lt>brE<gt>\n"
291              
292             =item B<html_table [ARGS]>
293              
294             Returns an HTML table containing the checkboxes. The table is constructed according ARGS, which are name/value pairs. Valid arguments are:
295              
296             =over 4
297              
298             =item class
299              
300             The value of the "table" tag's "class" HTML attribute. Defaults to C<checkbox-group>. Any value passed for this attribute joined to C<checkbox-group> with a single space.
301              
302             =item columns
303              
304             The number of columns in the table. Defaults to L<columns()|/columns>, or 1 if L<columns()|/columns> is false.
305              
306             =item format_item
307              
308             The name of the method to call on each checkbox object in order to fill each table cell. Defaults to "html"
309              
310             =item rows
311              
312             The number of rows in the table. Defaults to L<rows()|/rows>, or 1 if L<rows()|/rows> is false.
313              
314             =item table
315              
316             A reference to a hash of HTML attribute/value pairs to be used in the "table" tag.
317              
318             =item td
319              
320             A reference to a hash of HTML attribute/value pairs to be used in the "td" tag, or an array of such hashes to be used in order for the table cells of each row. If the array contains fewer entries than the number of cells in each row of the table, then the last entry is used for all of the remaining cells in the row. Defaults to a reference to an empty hash, C<{}>.
321              
322             =item tr
323              
324             A reference to a hash of HTML attribute/value pairs to be used in the "tr" tag, or an array of such hashes to be used in order for the table rows. If the array contains fewer entries than the number of rows in the table, then the last entry is used for all of the remaining rows. Defaults to a reference to an empty hash, C<{}>.
325              
326             =back
327              
328             Specifying "rows" and "columns" values (either as ARGS or via L<rows()|/rows> and L<columns()|/columns>) that are both greater than 1 leads to undefined behavior if there are not exactly "rows x columns" checkboxes. For predictable behavior, set either rows or columns to a value greater than 1, but not both.
329              
330             =item B<items_html_attr NAME [, VALUE]>
331              
332             This is an alias for the L<checkboxes_html_attr|/checkboxes_html_attr> method.
333              
334             =item B<label VALUE [, LABEL]>
335              
336             Get or set the label for the checkbox whose value is VALUE. The label for that checkbox is returned. If the checkbox exists, but has no label, then the value itself is returned. If the checkbox does not exist, then undef is returned.
337              
338             =item B<labels [LABELS]>
339              
340             Get or set the labels for all checkboxes. 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.
341              
342             Returns a hash of value/label pairs in list context, or a reference to a hash in scalar context.
343              
344             =item B<linebreak [BOOL]>
345              
346             Get or set the flag that determines whether or not the string stored in L<html_linebreak()|/html_linebreak> or L<xhtml_linebreak()|/xhtml_linebreak> is used to separate checkboxes in the output of L<html()|/html> or L<xhtml()|/xhtml>, respectively. Defaults to true.
347              
348             =item B<rows [ROWS]>
349              
350             Get or set the default number of rows to use in the output of the L<html_table()|/html_table> and L<xhtml_table()|/xhtml_table> methods.
351              
352             =item B<show_all_checkboxes>
353              
354             Set L<hidden|Rose::HTML::Form::Field::Checkbox/hidden> to false for all L<checkboxes|/checkboxes>.
355              
356             =item B<value [VALUE]>
357              
358             Simply calls L<input_value()|Rose::HTML::Form::Field/input_value>, passing all arguments.
359              
360             =item B<values [VALUE]>
361              
362             Simply calls L<input_value()|Rose::HTML::Form::Field/input_value>, passing all arguments.
363              
364             =item B<value_label [VALUE [, LABEL]]>
365              
366             If no arguments are passed, it returns the label of the first selected checkbox (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 checkbox is selected, undef is returned.
367              
368             With arguments, it will get or set the label for the checkbox whose value is VALUE. The label for that checkbox is returned. If the checkbox exists, but has no label, then the value itself is returned. If the checkbox does not exist, then undef is returned.
369              
370             =item B<value_labels>
371              
372             Returns an array (in list context) or reference to an array (in scalar context) of the labels of the selected checkboxes. If a checkbox has no label, the checkbox value is substituted. If no checkboxes are selected, then an empty array (in list context) or reference to an empty array (in scalar context) is returned.
373              
374             =item B<xhtml>
375              
376             Returns the XHTML for checkbox group, which consists of the L<xhtml()|/xhtml> for each checkbox object joined by L<xhtml_linebreak()|/xhtml_linebreak> if L<linebreak()|/linebreak> is true, or single spaces if it is false.
377              
378             =item B<xhtml_linebreak [XHTML]>
379              
380             Get or set the XHTML linebreak string. The default is "E<lt>br /E<gt>\n"
381              
382             =item B<xhtml_table>
383              
384             Equivalent to L<html_table()|/html_table> but using XHTML markup for each checkbox.
385              
386             =back
387              
388             =head1 AUTHOR
389              
390             John C. Siracusa (siracusa@gmail.com)
391              
392             =head1 LICENSE
393              
394             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.