3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 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 = [
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([
151 'haystack' => [$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(),
172 if ($validator = $this->getValidator()) {
173 $spec['validators'] = [
182 * Checks if this checkbox is checked.
186 public function isChecked()
188 return $this->value
=== $this->getCheckedValue();
192 * Checks or unchecks the checkbox.
194 * @param bool $value The flag to set.
197 public function setChecked($value)
199 $this->value
= $value ?
$this->getCheckedValue() : $this->getUncheckedValue();
204 * Checks or unchecks the checkbox.
206 * @param mixed $value A boolean flag or string that is checked against the "checked value".
209 public function setValue($value)
211 // Cast to strings because POST data comes in string form
212 $checked = (string) $value === (string) $this->getCheckedValue();
213 $this->value
= $checked ?
$this->getCheckedValue() : $this->getUncheckedValue();