We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

checkboxes not working correctly?

OK so I have a form that has a checkbox:

$death=new Check('death');
$death->setAttribute('placeholder','Death');
$this->add($death);

When the page renders the form handle looks like this:

<input type="checkbox" id="death" name="death" placeholder="Death" />

when the form is submitted, in the controller if the checkbox is not checked the variable is not passed in $_POST which is as expected and standard HTML behavior. But when the checkbox is checked it is passing back the number 0 instead of "on" or the number 1. Is this a bug?



2.1k

$death->setAttribute('value', 1); ?



40.7k
edited Feb '15

yup that worked. But shouldn't that be implicit?



33.8k

No, that isn't implicit, because the checkbox will have a null default value (but the fact is that you getted a 0 instead of "", intval(..) maybe?)



2.1k

null = 0, and value unset = null when checked. so when u do a vardump it will give u a 0, however if u do a if $value === null it will pass but $value === 0 will fail i believe =d