3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Form\Element
;
13 use Zend\Form\Element
;
14 use Zend\InputFilter\InputProviderInterface
;
15 use Zend\Validator\InArray
as InArrayValidator
;
17 class Checkbox
extends Element
implements InputProviderInterface
24 protected $attributes = array(
29 * @var \Zend\Validator\ValidatorInterface
36 protected $useHiddenElement = true;
41 protected $uncheckedValue = '0';
46 protected $checkedValue = '1';
49 * Accepted options for MultiCheckbox:
50 * - use_hidden_element: do we render hidden element?
51 * - unchecked_value: value for checkbox when unchecked
52 * - checked_value: value for checkbox when checked
54 * @param array|Traversable $options
57 public function setOptions($options)
59 parent
::setOptions($options);
61 if (isset($options['use_hidden_element'])) {
62 $this->setUseHiddenElement($options['use_hidden_element']);
65 if (isset($options['unchecked_value'])) {
66 $this->setUncheckedValue($options['unchecked_value']);
69 if (isset($options['checked_value'])) {
70 $this->setCheckedValue($options['checked_value']);
77 * Do we render hidden element?
79 * @param bool $useHiddenElement
82 public function setUseHiddenElement($useHiddenElement)
84 $this->useHiddenElement
= (bool) $useHiddenElement;
89 * Do we render hidden element?
93 public function useHiddenElement()
95 return $this->useHiddenElement
;
99 * Set the value to use when checkbox is unchecked
101 * @param $uncheckedValue
104 public function setUncheckedValue($uncheckedValue)
106 $this->uncheckedValue
= $uncheckedValue;
111 * Get the value to use when checkbox is unchecked
115 public function getUncheckedValue()
117 return $this->uncheckedValue
;
121 * Set the value to use when checkbox is checked
123 * @param $checkedValue
126 public function setCheckedValue($checkedValue)
128 $this->checkedValue
= $checkedValue;
133 * Get the value to use when checkbox is checked
137 public function getCheckedValue()
139 return $this->checkedValue
;
145 * @return \Zend\Validator\ValidatorInterface
147 protected function getValidator()
149 if (null === $this->validator
) {
150 $this->validator
= new InArrayValidator(array(
151 'haystack' => array($this->checkedValue
, $this->uncheckedValue
),
155 return $this->validator
;
159 * Provide default input rules for this element
161 * Attaches the captcha as a validator.
165 public function getInputSpecification()
168 'name' => $this->getName(),
170 'validators' => array(
171 $this->getValidator()
179 * Checks if this checkbox is checked.
183 public function isChecked()
185 return $this->value
=== $this->getCheckedValue();
189 * Checks or unchecks the checkbox.
191 * @param bool $value The flag to set.
194 public function setChecked($value)
196 $this->value
= $value ?
$this->getCheckedValue() : $this->getUncheckedValue();
201 * Checks or unchecks the checkbox.
203 * @param mixed $value A boolean flag or string that is checked against the "checked value".
206 public function setValue($value)
208 // Cast to strings because POST data comes in string form
209 $checked = (string) $value === (string) $this->getCheckedValue();
210 $this->value
= $checked ?
$this->getCheckedValue() : $this->getUncheckedValue();