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