7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
22 /** Zend_Form_Element_Xhtml */
23 require_once 'Zend/Form/Element/Xhtml.php';
26 * Checkbox form element
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license http://framework.zend.com/license/new-bsd New BSD License
35 class Zend_Form_Element_Checkbox
extends Zend_Form_Element_Xhtml
38 * Is the checkbox checked?
41 public $checked = false;
44 * Use formCheckbox view helper by default
47 public $helper = 'formCheckbox';
50 * Options that will be passed to the view helper
53 public $options = array(
54 'checkedValue' => '1',
55 'uncheckedValue' => '0',
62 protected $_checkedValue = '1';
65 * Value when not checked
68 protected $_uncheckedValue = '0';
74 protected $_value = '0';
79 * Intercept checked and unchecked values and set them early; test stored
80 * value against checked and unchecked values after configuration.
82 * @param array $options
83 * @return Zend_Form_Element_Checkbox
85 public function setOptions(array $options)
87 if (array_key_exists('checkedValue', $options)) {
88 $this->setCheckedValue($options['checkedValue']);
89 unset($options['checkedValue']);
91 if (array_key_exists('uncheckedValue', $options)) {
92 $this->setUncheckedValue($options['uncheckedValue']);
93 unset($options['uncheckedValue']);
95 parent
::setOptions($options);
97 $curValue = $this->getValue();
98 $test = array($this->getCheckedValue(), $this->getUncheckedValue());
99 if (!in_array($curValue, $test)) {
100 $this->setValue($curValue);
109 * If value matches checked value, sets to that value, and sets the checked
112 * Any other value causes the unchecked value to be set as the current
113 * value, and the checked flag to be set as false.
116 * @param mixed $value
117 * @return Zend_Form_Element_Checkbox
119 public function setValue($value)
121 if ($value == $this->getCheckedValue()) {
122 parent
::setValue($value);
123 $this->checked
= true;
125 parent
::setValue($this->getUncheckedValue());
126 $this->checked
= false;
134 * @param string $value
135 * @return Zend_Form_Element_Checkbox
137 public function setCheckedValue($value)
139 $this->_checkedValue
= (string) $value;
140 $this->options
['checkedValue'] = $value;
145 * Get value when checked
149 public function getCheckedValue()
151 return $this->_checkedValue
;
155 * Set unchecked value
157 * @param string $value
158 * @return Zend_Form_Element_Checkbox
160 public function setUncheckedValue($value)
162 $this->_uncheckedValue
= (string) $value;
163 $this->options
['uncheckedValue'] = $value;
168 * Get value when not checked
172 public function getUncheckedValue()
174 return $this->_uncheckedValue
;
181 * @return Zend_Form_Element_Checkbox
183 public function setChecked($flag)
185 $this->checked
= (bool) $flag;
186 if ($this->checked
) {
187 $this->setValue($this->getCheckedValue());
189 $this->setValue($this->getUncheckedValue());
199 public function isChecked()
201 return $this->checked
;