4

Here is the form helper I have used for checkbox

<?php
echo $this->Form->input('name',array('type'=>'checkbox','options'=>$options));
?>

and $options array is as follows:

 [options] => Array
                        (
                            [58] => 58
                            [85] => 85
                        )

But I am getting only one check box with both values in it. How can I get check box for each values.

3
  • 1
    You will need a foreach() Commented Sep 5, 2013 at 13:42
  • 1
    @Marijke We can do it without foreach() also.. I have marked correct answer which worked Commented Sep 5, 2013 at 13:47
  • Yup, that's a solution too. Commented Sep 5, 2013 at 13:48

3 Answers 3

5

Use the multiple attribute.

   echo $this->Form->input('Name',array(
        'label' => __('Label',true),
        'type' => 'select',
        'multiple' => 'checkbox',
        'options' => $options,
    ));
Sign up to request clarification or add additional context in comments.

Comments

0

Another thing you have to check, and this is truly a general rule in cakephp when things do not run as expected. is:

"Are you properly closing the form? Do your inputs stay inside <form>...</form>? If you are not sure how to check simply use your preferred DevTool and check the rendered HTML page.

This is almost the thing I forgot to check mostly and which always let me waste a lot of time!

Comments

0

If your are creating the $option variable in the view this will help you :

$options = array("key" => "value" , "key" => "value" , "key" => "value");

But if you are setting it the controller this will help you :

$this->set('options', array("key" => "value" , "key" => "value" , "key" => "value"));
  • key is the value in each option of the select input
  • value is the text of the option tags

Comments

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.