0
votes

I'm not doing anything fancy with habtm.

What i'm trying to do is have multiple checkboxes in cakephp, and, well, use them.

So i have this code in my view:

<div class="deleteButton">
    <?php echo $form->input('', array('type'=>'checkbox',
    'value'=>$video['Video']['id'], 'label'=>false, 'hiddenField'=>false,
    'id'=>false, 'multiple'=>true)); ?>
</div>

and this gives me this html in the rendered page:

<input type="checkbox" value="30" name="data[Videos]">
<input type="checkbox" value="31" name="data[Videos]">
<input type="checkbox" value="32" name="data[Videos]">

Now, when i select this in the html and submit, i only get ONE value in the controller: ... pr($this->data); ...

Looks like cakephp is overwriting the values stored somehow, because the ONE value i get is always the last checked checkbox value.

What am i doing wrong?

UPDATE: Oh god i hate cakephp

Using this: http://nuts-and-bolts-of-cakephp.com/2008/05/05/multiple-checkboxes/

Or rather, this:: input($video['Video']['id'], array('type'=>'checkbox', 'value'=>$video['Video']['id'], 'label'=>false, 'hiddenField'=>false, 'multiple'=>true)); ?>

(i made $video['Video']['id'] the first entry, which is for ids, apparently)

My request is now black-holed by cakephp's completely ridic blackhole in the Security component. Honest, WHY doesn't the 404 page just have a toggleable error? Now i have to work out how to get a meaningful error from it...

any ideas are welcome!

UPDATE:SOLVED: Huh, instead I needed:

<?php echo $form->input("Video".$video['Video']['id']."id", array('type'=>'checkbox', 'value'=>$video['Video']['id'], 'label'=>false, 'hiddenField'=>false, 'multiple'=>true)); ?> 

That is, i wrapped the $video['Video']['id'] with "Video" and "id".

NFI why this works, anyone have any ideas, i'll mark you as right!

2

2 Answers

4
votes

Looks like cakephp is overwriting the values stored somehow, because the ONE value i get is always the last checked checkbox value.

it wasn't cake's fault... if you have two or more inputs with the same name (in your case 'data[Videos]') php will only use the last value to create the $_POST variable.. it like in a GET request if you do something like:

mysite.com/intex.php?attr=1&attr=2&attr=3

You solved it by adding the video id in the field name. So now the generated html is something like:

<input type="checkbox" value="30" name="data[Videos1]">
<input type="checkbox" value="31" name="data[Videos2]">
<input type="checkbox" value="32" name="data[Videos3]">

so as you can see,now all the input names are different and nothing gets overwritten.

However, instead of giving the names by yourself, you could use the 'multiple' option and the 'options' option to create all the checkboxes using only one statement. Something like:

echo $this->Form->input('video',array('options'=> array('Value 1'=>'Label 1',
                                                       'Value 2'=>'Label 2',
                                                       'Value 3'=>'Label 3'
                                                       ),
                                      'multiple' => 'checkbox'
        ));

It's all in the doc ;)

Hope this helps

0
votes

in the $form->input() first parameter is model.field. In first case you did not specified the field, so they all went in $this->data['Videos'] instead $this->data['Videos']['cb1'],$this->data['Videos']['cb2'],$this->data['Videos']['cb3'].

even you create the form with model:

$form->create('modelName'));

it is good habit to specify the field too:

$form->input('modelName.fieldname', array());