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?
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.
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.
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.
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.
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 ($('#TloSubscriptions_is_renewal_first').is(':checked')) {
$('#ytTloSubscriptions_is_renewal_first').val(1);
}