[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Form / Element / Checkbox.php
blob159c221ecb9e5a0103c1f3b92e1d26d8763aa5cf
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Form
17 * @subpackage Element
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';
25 /**
26 * Checkbox form element
28 * @category Zend
29 * @package Zend_Form
30 * @subpackage 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
33 * @version $Id$
35 class Zend_Form_Element_Checkbox extends Zend_Form_Element_Xhtml
37 /**
38 * Is the checkbox checked?
39 * @var bool
41 public $checked = false;
43 /**
44 * Use formCheckbox view helper by default
45 * @var string
47 public $helper = 'formCheckbox';
49 /**
50 * Options that will be passed to the view helper
51 * @var array
53 public $options = array(
54 'checkedValue' => '1',
55 'uncheckedValue' => '0',
58 /**
59 * Value when checked
60 * @var string
62 protected $_checkedValue = '1';
64 /**
65 * Value when not checked
66 * @var string
68 protected $_uncheckedValue = '0';
70 /**
71 * Current value
72 * @var string 0 or 1
74 protected $_value = '0';
76 /**
77 * Set options
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);
103 return $this;
107 * Set value
109 * If value matches checked value, sets to that value, and sets the checked
110 * flag to true.
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;
124 } else {
125 parent::setValue($this->getUncheckedValue());
126 $this->checked = false;
128 return $this;
132 * Set checked value
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;
141 return $this;
145 * Get value when checked
147 * @return string
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;
164 return $this;
168 * Get value when not checked
170 * @return string
172 public function getUncheckedValue()
174 return $this->_uncheckedValue;
178 * Set checked flag
180 * @param bool $flag
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());
188 } else {
189 $this->setValue($this->getUncheckedValue());
191 return $this;
195 * Get checked flag
197 * @return bool
199 public function isChecked()
201 return $this->checked;