Recent Posts
- (09/10) Fixing Warning: the ECDSA host key for 'github.com' differs from the key for the IP addressTAGS:Web Server Admin
- (12/26) CakePHP 3 - Getting List of Column Definitions from a Table (like schema())TAGS:CakephpCake3
- (09/14) Change Order of Loaded Behaviors in CakePHP 3TAGS:Cake3CakephpWeb ProgrammingPhp
- (05/29) CakePHP 3 - Accept JSON Header Only Working When Debug FalseTAGS:Web ProgrammingCakephpCake3
- (05/23) Remove All Events from Google Calendar (Reset Calendar)TAGS:Web ProgrammingPhp
- (11/08) Google Tag Manager (GTM) Not Firing Default PageView EventTAGS:Web ProgrammingJavascriptGoogle Tag Manager
- (10/13) In PHP, how do you get __toString() Magic Method Result without calling echo?TAGS:CakephpCake3Cakephp 13PhpWeb Programming
- (11/14) Getting output from shell_exec() at all timesTAGS:Web ProgrammingWeb Server Admin
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 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:
- 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
Comments for this Posting
Sorry, comments are closed for this posting.
Please Email Kevin if you have any questions. Thanks!
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.