richardwfrancis has asked for the wisdom of the Perl Monks concerning the following question:

Hello wise ones,

I'm having some trouble with CGI's checkbox_group function and more specifically the ability to assign attributes to the <label> tag using "-labelattributes"

Using this code for a single checkbox assigns the label with the css class as requested:

#!/usr/bin/perl use strict; use CGI qw/:standard/; my $cgi = new CGI; print $cgi->header; print $cgi->start_html; print $cgi->checkbox(-name => 'my_checkbox', -value => 'eenie', -label => 'first choice', -labelattributes => { class => 'somecssclass' } ); print $cgi->end_html;

When taking this to a checkbox_group, all labels appear with the same class:

#!/usr/bin/perl use strict; use CGI qw/:standard/; my $cgi = new CGI; print $cgi->header; print $cgi->start_html; my %labels = ( 'eenie'=>'first choice', 'meenie'=>'second choice', 'minie'=>'third choice'); print $cgi->checkbox_group(-name => 'my_checkbox', -values => ['eenie','meenie','minie'], -labels => \%labels, -labelattributes => { class => 'somecssclass' } ); print $cgi->end_html;

What I want to do is similar to the way "-attributes" works, so as to be able to control *which* labels I assign attributes to. Something like:

#!/usr/bin/perl use strict; use CGI qw/:standard/; my $cgi = new CGI; print $cgi->header; print $cgi->start_html; my %labels = ('eenie'=>'first choice', 'meenie'=>'second choice', 'minie'=>'third choice'); my %labelattr = ('eenie'=> { 'class' => 'somecssclass' } ); my %attr = ('eenie'=> { 'class' => 'someothercssclass' } ); print $cgi->checkbox_group(-name => 'my_checkbox', -values => ['eenie','meenie','minie'], -labels => \%labels, -attributes => \%attr, -labelattributes => \%labelattr ); print $cgi->end_html;
However when I do this I get the following, which given the previous code I kind of expected but don't really want! Note that the <input> tag for the value 'eenie' gets assigned the right class but ALL labels get assigned some unwanted eenie=hashref garbage!
<label eenie="HASH(0x13e95e8)"><input type="checkbox" name="my_checkbo +x" value="eenie" class="someothercssclass"/>first choice</label><lab +el eenie="HASH(0x13e95e8)"><input type="checkbox" name="my_checkbox" +value="meenie" />second choice</label><label eenie="HASH(0x13e95e8)"> +<input type="checkbox" name="my_checkbox" value="minie" />third choic +e</label>

Is there any way to specifically assign attributes to specific labels using cgi and checkbox_group?

Many many thanks for your help.

Rich

Replies are listed 'Best First'.
Re: Using labelattributes with CGI's checkbox_group
by Anonymous Monk on Feb 26, 2011 at 08:27 UTC
    Congratulations, you've found another bug in CGI.pm's html generation subroutines, hooray, you should report it :)
    #!/usr/bin/perl -- use strict; use warnings; use CGI::Pretty; my %labels = ( 'eenie' => 'your first choice', 'meenie' => 'your second choice', 'minie' => 'your third choice' ); print "\n------ broken (the example in the docs), unsurprising, its un +tested ----------\n"; print CGI::Pretty->checkbox_group( 'group_name', [ 'eenie', 'meenie', 'minie', 'moe' ], [ 'eenie', 'moe' ], 'true', \%labels, { 'moe' => { 'class' => 'red' }, 'eenie' => { 'class' => 'yo' }, } ); print "\n----- works (also documented but untested) -----------\n"; print CGI::Pretty->checkbox_group( -name => 'group_name', -values => [ 'eenie', 'meenie', 'minie', 'moe' ], -default => [ 'eenie', 'moe' ], -linebreak => 'true', -labels => \%labels, -attributes => { 'moe' => { 'class' => 'red' }, 'eenie' => { 'class' => 'yo' }, } ); __END__ ------ broken (the example in the docs), unsurprising, its untested -- +-------- <label eenie="HASH(0xa7d7a4)" moe="HASH(0xa7d6b4)"> <input type="checkbox" name="group_name" value="eenie" checked +="checked" />your first choice </label> <br /><label eenie="HASH(0xa7d7a4)" moe="HASH(0xa7d6b4)"> <input type="checkbox" name="group_name" value="meenie" />your + second choice </label> <br /><label eenie="HASH(0xa7d7a4)" moe="HASH(0xa7d6b4)"> <input type="checkbox" name="group_name" value="minie" />your +third choice </label> <br /><label eenie="HASH(0xa7d7a4)" moe="HASH(0xa7d6b4)"> <input type="checkbox" name="group_name" value="moe" checked=" +checked" />moe </label> <br /> ----- works (also documented but untested) ----------- <label> <input type="checkbox" name="group_name" value="eenie" checke +d="checked" class="yo"/>your first choice </label> <br /><label> <input type="checkbox" name="group_name" value="meenie" />you +r second choice </label> <br /><label> <input type="checkbox" name="group_name" value="minie" />your + third choice </label> <br /><label> <input type="checkbox" name="group_name" value="moe" checked= +"checked" class="red"/>moe </label> <br />

      Thanks!

      I just did.