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
;
12 use Zend\Form\ElementInterface
;
13 use Zend\Form\Exception\InvalidArgumentException
;
14 use Zend\Validator\Explode
as ExplodeValidator
;
15 use Zend\Validator\InArray
as InArrayValidator
;
16 use Zend\Validator\ValidatorInterface
;
18 class MultiCheckbox
extends Checkbox
25 protected $attributes = array(
26 'type' => 'multi_checkbox',
32 protected $useHiddenElement = false;
37 protected $uncheckedValue = '';
42 protected $valueOptions = array();
47 public function getValueOptions()
49 return $this->valueOptions
;
53 * @param array $options
54 * @return MultiCheckbox
56 public function setValueOptions(array $options)
58 $this->valueOptions
= $options;
60 // Update Explode validator haystack
61 if ($this->validator
instanceof ExplodeValidator
) {
62 $validator = $this->validator
->getValidator();
63 $validator->setHaystack($this->getValueOptionsValues());
70 * Set options for an element. Accepted options are:
71 * - label: label to associate with the element
72 * - label_attributes: attributes to use when the label is rendered
73 * - value_options: list of values and labels for the select options
75 * @param array|\Traversable $options
76 * @return MultiCheckbox|ElementInterface
77 * @throws InvalidArgumentException
79 public function setOptions($options)
81 parent
::setOptions($options);
83 if (isset($this->options
['value_options'])) {
84 $this->setValueOptions($this->options
['value_options']);
86 // Alias for 'value_options'
87 if (isset($this->options
['options'])) {
88 $this->setValueOptions($this->options
['options']);
95 * Set a single element attribute
99 * @return MultiCheckbox|ElementInterface
101 public function setAttribute($key, $value)
103 // Do not include the options in the list of attributes
104 // TODO: Deprecate this
105 if ($key === 'options') {
106 $this->setValueOptions($value);
109 return parent
::setAttribute($key, $value);
115 * @return ValidatorInterface
117 protected function getValidator()
119 if (null === $this->validator
) {
120 $inArrayValidator = new InArrayValidator(array(
121 'haystack' => $this->getValueOptionsValues(),
124 $this->validator
= new ExplodeValidator(array(
125 'validator' => $inArrayValidator,
126 'valueDelimiter' => null, // skip explode if only one value
129 return $this->validator
;
133 * Get only the values from the options attribute
137 protected function getValueOptionsValues()
140 $options = $this->getValueOptions();
141 foreach ($options as $key => $optionSpec) {
142 $value = (is_array($optionSpec)) ?
$optionSpec['value'] : $key;
145 if ($this->useHiddenElement()) {
146 $values[] = $this->getUncheckedValue();
152 * Sets the value that should be selected.
154 * @param mixed $value The value to set.
155 * @return MultiCheckbox
157 public function setValue($value)
159 $this->value
= $value;