Recent Posts
- (02/24) Calling an Element from a Helper TAGS:CakephpWeb ProgrammingUsageTutorialExample
- (10/14) Forcing A Single Join in CakePHP PaginationTAGS:CakephpCakephp 13DatabaseErrorsHabtmMysqlMssqlWeb ProgrammingUsage
- (08/11) Inserting NOW() into MySQL Using CakePHPTAGS:CakephpDatabaseMysqlMssqlWeb ProgrammingUsage
- (08/07) Best PaginateCount for CakePHP - with Group By SupportTAGS:CakephpBehaviorsWeb ProgrammingUsageDatabaseExampleMysqlHabtmHack
- (04/03) CakePHP Error Messages Not Showing on FormTAGS:CakephpCakephp 13Web ProgrammingErrorsUsage
- (02/06) Sorting Paginated Results Using a Related Model Field in CakePHPTAGS:CakephpWeb ProgrammingUsageExample
- (11/02) Changing CakePHP's Model useTable on the FlyTAGS:CakephpWeb ProgrammingMysqlDatabaseExampleTutorial
- (10/18) The Funniest Error Message Ever - Thank You EclipseTAGS:EclipseWeb ProgrammingErrorsHumorWindowsUsageSoftwarePhpCakephp
Subscribe to my feed
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 14,838 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:
- 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
Tags for Create a Checklist Group or Related Checkboxes in CakePHP
Cakephp | Example | Tutorial | Usage | Web Programming

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.