File Coverage

blib/lib/Rose/HTML/Form/Field/RadioButtonGroup.pm
Criterion Covered Total %
statement 28 30 93.3
branch 7 8 87.5
condition 2 4 50.0
subroutine 9 11 81.8
pod 3 3 100.0
total 49 56 87.5


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