0

I'm trying to make a checkbox group in Perl with sub-titles dividing certain checkboxes. All the checkboxes are related, but they are in subcategories that I would like to be displayed when the user is selecting their choices.

my $grocery_list = $q->checkbox_group(
-name=>'grocery_list',
-values=>\@items,
-linebreak=>'true',
-labels=>\%items,

In the above example, I might have 'Milk' and 'Cheese' be under the subcategory of "DAIRY", while 'Ham' and 'Turkey' are under the subcategory of "MEATS". I already have a checkbox group with my values but I'm struggling creating those subcategory titles (DAIRY and MEATS) in between the subgroups of checkboxes. Ideally, the subcategory titles wouldn't be checkboxes, but would just divide checkboxes. Is it possible to put these subdivisions in a single checkbox, or would I have to make multiple checkboxes and merge the checked items into a single array afterward?

3
  • I don't know much about Perl, but what toolkit or library are you using to display these checkboxes? That is probably highly relevant to this question. Commented Mar 13, 2015 at 2:19
  • Ah you're very right! I'm just starting to learn Perl too and this is one of my first assignments for work. I'm creating a CGI checkbox group. perldoc.perl.org/… This is the link that I've been going off of but it isn't helping at all with the subgroupings. I hope this answers your question. Commented Mar 13, 2015 at 2:35
  • 1
    There's a newer version of that document on MetaCPAN - metacpan.org/pod/CGI. In particular, you should read the section entitled "HTML Generation functions should no longer be used". Commented Mar 13, 2015 at 14:16

1 Answer 1

1

Note that CGI is no longer considered to be best practice. You should read CGI::Alternatives for an explanation, as well as suggestions for alternative modules.

You need the fieldset and legend elements to do what you describe. Without any additional CSS, the former draws a box around the group of inputs that it contains, and the latter labels that box.

Unfortunately, the checkbox_group convenience method doesn't allow you to subdivide its elements between two field sets, so you will have to call it twice with the same parameters except for the values and labels. It may be better to write your own helper routine that calls checkbox directly to build appropriate grouping.

Here's the basic idea. There's nothing magical about the CGI methods -- they just generate HTML according to the parameters you pass.

my %labels = (
  milk   => 'Milk',
  cheese => 'Cheese',
  ham    => 'Ham',
  turkey => 'Turkey',
);

my @dairy_items = qw/ milk cheese /;
my @meat_items  = qw/ ham turkey /;

my $dairy = $q->checkbox_group(
  -name      => 'grocery_list',
  -values    => \@dairy_items,
  -linebreak => 'true',
  -labels    => \%labels,
);

my $meat = $q->checkbox_group(
  -name      => 'grocery_list',
  -values    => \@meat_items,
  -linebreak => 'true',
  -labels    => \%labels,
);

print
  $q->start_form,
    $q->fieldset(
      $q->legend('Dairy'),
      $dairy,
    ),
    $q->fieldset(
      $q->legend('Meat'),
      $meat,
    ),
  $q->end_form;
Sign up to request clarification or add additional context in comments.

1 Comment

Because I only needed dividers for 5 different groups, and the groups weren't all that long, I found it easiest to just write the HTML code for the individual groups and implement the checkboxes that way. Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.