MainelyDesign.com Blog

Create a Checklist Group or Related Checkboxes in CakePHP

Posted on 03/05/2010 at 12:38 pm by Kevin Wentworth
Viewed 43,723 times | 1 comment

This is an interesting one.  I needed to create a group of checkboxes.  In my mind, it's just like a radio button group, except with checkboxes.  The only difference should be that with radio buttons you can only select one, while with a checkbox group you should be able to select as many as you want.  To achieve a checklist group in CakePHP is a little convoluted.

First Step, Create an Input of type=>'select'

That's right, if you want to make a group of checkboxes that are related in CakePHP, you have to first create an input of type equals select through the form helper.

Next Step, Set multiple=>'checkbox'

Easy- just set the multiple parameter equal to checkbox.  Now CakePHP's form helper will output a group of checkboxes instead of a select drop down menu. For more information on what happens when you set 'multiple'=>true read the manual for $options['multiple'].

Last Step, Give Yourself Options

The last part of making a checklist group is to give the form helper the options you want to be outputted. Here's the full line of code:

  1. echo $form->input('Checkboxes', array('type'=>'select', 'multiple'=>'checkbox', 'options'=>array(1=>'One Value', 2=>'Two Value)));

My only gripe about this method is that each checkbox is wrapped in a div, then the whole checkbox group is wrapped in a div, instead of wrapping everything in a fieldset, like the radio button group.

Correct Me If I'm Wrong

I haven't really figured out how to customize the display of the checkbox group without overriding the form helper, so if you have any input please comment.

Cheers,
-Kevin Wentworth

Bookmark and Share

Tags for Create a Checklist Group or Related Checkboxes in CakePHP

Cakephp | Example | Tutorial | Usage | Web Programming

Comments for this Posting

Posted by Eric Wilson

on 18/10/10

echo $this->Form->input('letters', array(
'type' => 'select',
'options' => array('a', 'b', 'c'),
'multiple' => 'checkbox',
'selected' => array('a')
)
)

when cake 1.3 generates checkboxes it assigns the divs to a checkbox class so you can then style them w/o affecting your other input styles:
.checkbox
{
float: left;
padding-right: 15px;
}

the above would line them all up horizontally with some padding to avoid confusion.

Sorry, comments are closed for this posting.

Please Email Kevin if you have any questions. Thanks!

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design