- (11/14) Getting output from shell_exec() at all timesTAGS:Web ProgrammingWeb Server Admin
- (06/12) View Layer - Getting count of Entities in View in Cakephp 3TAGS:CakephpCake3
- (05/23) Changing URL in beforeRedirect() in Component - Cakephp 3TAGS:CakephpCake3Web Programming
- (01/28) Foundation CSS Framework ResourcesTAGS:FoundationCss
- (01/27) Change View File from Controller in CakePHP 3TAGS:CakephpCake3UsageWeb Programming
- (01/13) Loading Model/Table Anywhere in Cake3TAGS:Cake3Cakephp
- (10/06) CURL PHP 5.5 Issue on Windows (Using Twilio SDK)
Subscribe to my feed
Create a Checklist Group or Related Checkboxes in CakePHP
Posted on 03/05/2010 at 12:38 pm by Kevin Wentworth
Viewed 35,765 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.