| HTML-Widget documentation | Contained in the HTML-Widget distribution. |
HTML::Widget::Element::RadioGroup - Radio Element grouping
my $e = $widget->element( 'RadioGroup', 'foo' );
$e->comment('(Required)');
$e->label('Foo'); # label for the whole thing
$e->values([qw/foo bar gorch/]);
$e->labels([qw/Fu Bur Garch/]); # defaults to ucfirst of values
$e->comments([qw/funky/]); # defaults to empty
$e->value("foo"); # the currently selected value
$e->constrain_values(1);
RadioGroup Element.
As of version 1.09, an In constraint is no longer automatically added to RadioGroup elements. Use constrain_values to provide this functionality.
Add a comment to this Element.
This label will be placed next to your Element.
Because the RadioGroup is placed in a fieldset tag, you can also set a
</legend> value. Note, however, that if you want the RadioGroup to be styled
the same as other elements, the label setting is recommended.
List of form values for radio checks. Will also be used as labels if not otherwise specified via labels.
The labels for corresponding values.
If true, an In constraint will automatically be added to the widget, using the values from values.
If true, overrides the default behaviour, so that after a field is missing from the form submission, the xml output will contain the default value, rather than be empty.
To horizontally align the radio buttons with the label, use the following CSS.
.radiogroup > label {
display: inline;
}
A RadioGroup is now rendered using a fieldset tag, instead of a label
tag. This is because the individual radio buttons also use labels, and the
W3C xhtml specification forbids nested label tags.
To ensure RadioGroup elements are styled similar to other elements, you must
change any CSS label definitions to also target the RadioGroup's class.
This means changing any label { ... } definition to
label, .radiogroup_fieldset { ... }. If you're using the simple.css
example file, testing with firefox shows you'll also need to add
margin: 0em; to that definition to get the label to line up with other
elements.
If you find the RadioGroup fieldset picking up styles intended only for
other fieldsets, you can either override those styles with your
label, .radiogroup_fieldset { ... } definition, or you can change your
fieldset { ... } definition to .widget_fieldset{ ... } to specifically
target any Fieldset elements other than the RadioGroup's.
Previously, if there were any errors, the label tag was given the
classname labels_with_errors. Now, if there's errors, the RadioGroup
fieldset tag is wrapped in a span tag which is given the classname
labels_with_errors. To ensure that any labels_with_errors styles are
properly displayed around RadioGroups, you must add display: block; to
your .labels_with_errros{ ... } definition.
Jess Robinson
Yuval Kogman
This library is free software, you can redistribute it and/or modify it under the same terms as Perl itself.
| HTML-Widget documentation | Contained in the HTML-Widget distribution. |
package HTML::Widget::Element::RadioGroup; use warnings; use strict; use base 'HTML::Widget::Element'; *value = \&checked; __PACKAGE__->mk_accessors( qw/ comment label values labels comments checked _current_subelement constrain_values legend retain_default/ );
sub new { my ( $class, $opts ) = @_; my $self = $class->NEXT::new($opts); my $values = $opts->{values}; $self->values($values); $self; }
sub prepare { my ( $self, $w, $value ) = @_; if ( $self->constrain_values ) { my $name = $self->name; my %seen; my @uniq = grep { !$seen{$_}++ } @{ $self->values }; $w->constraint( 'In', $name )->in(@uniq) if @uniq; } return; }
sub containerize { my ( $self, $w, $value, $errors, $args ) = @_; $value = $self->value if ( not defined $value ) and $self->retain_default || not $args->{submitted}; $value = '' if not defined $value; my $name = $self->name; my @values = @{ $self->values || [] }; my @labels = @{ $self->labels || [] }; @labels = map {ucfirst} @values unless @labels; my @comments = @{ $self->comments || [] }; my $i; my @radios = map { $self->_current_subelement( ++$i ); # yucky hack my $radio = $self->mk_input( $w, { type => 'radio', ( $_ eq $value ? ( checked => "checked" ) : () ), value => $_, } ); $radio->attr( class => "radio" ); my $label = $self->mk_label( $w, shift @labels, shift @comments ); $label->unshift_content($radio); $label; } @values; $self->_current_subelement(undef); my $fieldset = HTML::Element->new('fieldset'); $fieldset->attr( class => 'radiogroup_fieldset' ); my $outer_id = $self->attributes->{id} || $self->id($w); if ( defined $self->legend ) { my $legend = HTML::Element->new('legend'); $legend->attr( class => 'radiogroup_legend' ); $legend->push_content( $self->legend ); $fieldset->push_content($legend); } # don't pass commment to mk_label, we'll handle it ourselves my $l = $self->mk_label( $w, $self->label, undef, $errors ); if ($l) { $l->tag('span'); $l->attr( for => undef ); $l->attr( class => 'radiogroup_label' ); } if ( defined $self->comment ) { my $c = HTML::Element->new( 'span', id => "$outer_id\_comment", class => 'label_comments' ); $c->push_content( $self->comment ); $fieldset->push_content($c); } my $element = HTML::Element->new('span'); $element->attr( class => 'radiogroup' ); $element->push_content(@radios); $fieldset->push_content($element); if ($errors) { my $save = $fieldset; $fieldset = HTML::Element->new('span'); $fieldset->attr( class => 'labels_with_errors' ); $fieldset->attr( id => $outer_id ); $fieldset->push_content($save); } else { $fieldset->attr( id => $outer_id ); } return $self->container( { element => $fieldset, error => scalar $self->mk_error( $w, $errors ), label => $l, } ); }
sub id { my ( $self, $w ) = @_; my $id = $self->SUPER::id($w); my $subelem = $self->_current_subelement; return $subelem ? "${id}_$subelem" : $id; }
1;