Disabled checked checkbox returns "0"

Hi!

On an admin page, I have a checkbox. In order to avoid accidental change, I disabled it (‘htmloptions’=>array(‘disabled=>disabled’)). So when I really need to set it, I can enable it (using my browser) an change the value.

However, a disabled checkbox always returns "0", regardless if it is checked or not.

Can one have the behaviour: the checkbox sends its value, regardless wether disabled or not?

Why do you need it’s value if the value is always the same? You can set it in your controller anywhere.

You can also use a hidden input or make this checkbox enabled but on click event prompt a user that he can’t uncheck it.

The value isn’t always the same. It’s either checked or unchecked when I load the page. And it’s either checked or unchecked (regardless what it was before) when I submit the form.

I wrote (with minor changes to make it even more clear):

On an admin page, I have a checkbox. In order to avoid accidental change, I disabled it. So when I really need to change its value, I can enable it (using my browser) and change the value.

Couldn’t you set it to readonly?

Good idea. But contrairy to how I understand the specification (http://www.htmlcodetutorial.com/forms/_INPUT_DISABLED.html "cannot bemodified by the user"), checkboxes do not obey readonly in FF and IE (e.g. http://www.htmlcodetutorial.com/forms/_INPUT_DISABLED.html).

As it says on that page

That’s why you get always 0 for that field…

One idea would be to leave the checkbox active so that you get the value and set it as hidden (with jQuery or CSS)…

I think what Spyros meant, was to make it read only in the model. That means: Don’t include that attribute in your scenario so it can’t be overwritten when you massively assing your attributes.

Having it readonly does not solve my problem. Is my problem so hard to understand?

  1. Beware of ‘disabled’=>‘disabled’. It may lead to unwanted behaviour.

  2. My solution for the problem mentioned above: ‘htmloptions’=>array(‘confirm’=>‘Are your sure to …?’) attached to the checkbox

There is another way to solve this problem. If you want the checkbox disabled, then you can pass ‘uncheckValue’ => 1 as an element in the $htmlOptions array. This will work.

u can try this :

$htmlOptions=array(‘DISABLED’=>true,)

Actually as someone said ‘disabled’ elements are not sent back to the server.

Just had a problem like that, I looked the $_POST array and disabled values were not there. In my case I changed to ReadOnly, however ReadOnly elements look the same as normal ones, while disabled elements have a different color (which helps the user locating them).

One solution could be to enabled them just before submitting (for example in the onSubmit of the form, or onClick of the submit button)

I still don’t understand, why you don’t solve the problem on model level: Just don’t make this attribute safe in the model (a.k.a. don’t define a rule in the current scenario) - and it will never be changeable. Then you don’t have to care wether it’s sent in POST or not.

I too faced a similar situation. I could solve this by setting value to the hidden field corresponding to the checkbox using jquery. On the submit button I called a js function to set the hidden value like the below.

<?php echo CHtml::submitButton($model->isNewRecord ? ‘Create’ : ‘Save’, array(‘onClick’=>‘chkboxVals()’)); ?>

And the js function to set the value is as follows

<script type="text/javascript">

function chkboxVals()

{

//#TloSubscriptions_is_renewal_first is the id for the checkbox and #ytTloSubscriptions_is_renewal_first is the id of hidden field corresponding to the chkbox. So if the chkbox is checked the the hidden value is set.





    if (&#036;('#TloSubscriptions_is_renewal_first').is(':checked')) {


	&#036;('#ytTloSubscriptions_is_renewal_first').val(1);


}

}

</script>