ZF-2750: Zend_Form_Element_Checkbox - setValue inconsistent with getValue

Issue Type: Bug Created: 2008-02-28T07:31:13.000+0000 Last Updated: 2008-03-21T16:25:46.000+0000 Status: Resolved Fix version(s): - 1.5.0 (17/Mar/08)

Reporter: ?ukasz Adamczak (czak) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Form

Related issues: Attachments:


When using Zend_Form_Element_Checkbox elements, getValue returns either 1 (checked) or 0 (unchecked). I find that great. However, the current setValue works differently:

<pre class="highlight">
public function setValue($value)
    $value = ($value === null) ? 0 : 1;
    $this->checked = ($value === 0) ? false : true;
    return parent::setValue($value);

Anything other than null, sets the checkbox value to 1 (even 0 or '0'). I would suggest keeping it consistent with getValue, something along these lines:

<pre class="highlight">
public function setValue($value)
    if ($value === null) $value = 0;
    $this->checked = ($value == 0) ? false : true;
    return parent::setValue($value);

I would also suggest allowing the value to be a string ('0' or '1') -> thus a ==. This will allow populating with the data straight from the DB.


Posted by Matthew Weier O'Phinney (matthew) on 2008-03-05T10:31:35.000+0000

This is one symptom of multiple issues with checkboxes. I will be fixing these for the 1.5.0GA release.

Posted by Matthew Weier O'Phinney (matthew) on 2008-03-07T11:39:28.000+0000

All checkbox issues are corrected in trunk and 1.5 release branch.


  • Set value of element when checked using setCheckedValue()
  • Set value of element when unchecked using setUncheckedValue()
  • setValue() then checks the submitted value against the checkedValue; if equal, that value is set; if not, the uncheckedValue is used. The checked property is then set accordingly
  • setChecked() will call setValue with either the checked or unchecked value based on the value of the flag
  • ViewHelper decorator always uses checked value to populate the form input (and uses the 'checked' property to determine whether or not it is on)

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.