aditional hidden fields with checkbox

Hi guys, Can you tell me why yii adds hidden inputs to the form when display a checBox??

Why are there that hiden field "yt"?? What is for there??

The code:

$metasCheck[‘style’]=“margin:4px; float:left”;

$chkAceptoRecibir=$form->checkBox($miembro,‘acepto_recibir’,$metasCheck);

The result

<input id="ytMiembro_acepto_recibir" type="hidden" value="0" name="Miembro[acepto_recibir]" />

<input name="Miembro[acepto_recibir]" id="Miembro_acepto_recibir" value="1" type="checkbox" />

Thanks

it is becuse when checkbox is not checked, referred value is not send to server on submit. When it is checked - it will overwrite value from hidden field.

This way you can have "unchecked" value.

thanks!!

Well, I don’t understand how the browser knows overwrite the value. Is required some plugin? If I just only render a form without javascript these “overwrite happens”?

I’m pretty sure the browser has nothing to do with that, since this happens in the server-side.

It’s the first time I read about this feature, don’t know if it overrides the value automatically when using $model->attributes or you have to manually set the attribute value with the value of the unchecked input, to figure it out you could try and see what you get by setting the uncheckvalue to a random value and then dumping your model attributes value after doing the $model->attributes statement in your controller. If your model attribute has the same value you defined for the checkbox uncheckvalue option, then it’s done automatically.

http://www.yiiframework.com/doc/api/1.1/CHtml#activeCheckBox-detail

it has nothing to do with Yii, it is the way PHP parses requests. if you call your script like this:

http://server/index.php?param=1&param=2&param=3

then you will get only one visible "param" with value of "3" (the last one).

hehe thanks redguy I’ve just understand that thanks to your comments

Me too.

I didn’t know the trick of hidden fields for checkboxes. And I had to reconstruct the values of them using something like “$value = (isset($something) && $something === true);”.

It could have been much easier with this trick. ::)

Please note that this feature is nice … unless you want to store values into array. Then you end up with something like this:


<input id="ytTelefon_0_mms" type="hidden" value="" name="Telefon[][mms]">

<input name="Telefon[][mms]" id="Telefon_0_mms" value="" type="checkbox">

In this case your indexes can get pretty messed up and you can spend hours figuring it out.

You can prevent generating of the hidden field by setting ‘uncheckValue’=>null in htmloptions array.

Who though that such hidden feature would be a great idea?

It’s a design error.

You ask for a checkbox and you get by default: a checkbox and a hidden checkbox.

This should be explicitly stated by a programmer that such hidden checkbox is created.