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\Form\ElementInterface
;
15 use Zend\Form\Exception\InvalidArgumentException
;
16 use Zend\InputFilter\InputProviderInterface
;
17 use Zend\Validator\Explode
as ExplodeValidator
;
18 use Zend\Validator\InArray
as InArrayValidator
;
20 class Select
extends Element
implements InputProviderInterface
27 protected $attributes = array(
32 * @var \Zend\Validator\ValidatorInterface
39 protected $disableInArrayValidator = false;
42 * Create an empty option (option with label but no value). If set to null, no option is created
46 protected $emptyOption = null;
51 protected $valueOptions = array();
56 public function getValueOptions()
58 return $this->valueOptions
;
62 * @param array $options
65 public function setValueOptions(array $options)
67 $this->valueOptions
= $options;
69 // Update InArrayValidator validator haystack
70 if (null !== $this->validator
) {
71 if ($this->validator
instanceof InArrayValidator
) {
72 $validator = $this->validator
;
74 if ($this->validator
instanceof ExplodeValidator
75 && $this->validator
->getValidator() instanceof InArrayValidator
77 $validator = $this->validator
->getValidator();
79 if (!empty($validator)) {
80 $validator->setHaystack($this->getValueOptionsValues());
88 * Set options for an element. Accepted options are:
89 * - label: label to associate with the element
90 * - label_attributes: attributes to use when the label is rendered
91 * - value_options: list of values and labels for the select options
92 * _ empty_option: should an empty option be prepended to the options ?
94 * @param array|Traversable $options
95 * @return Select|ElementInterface
96 * @throws InvalidArgumentException
98 public function setOptions($options)
100 parent
::setOptions($options);
102 if (isset($this->options
['value_options'])) {
103 $this->setValueOptions($this->options
['value_options']);
105 // Alias for 'value_options'
106 if (isset($this->options
['options'])) {
107 $this->setValueOptions($this->options
['options']);
110 if (isset($this->options
['empty_option'])) {
111 $this->setEmptyOption($this->options
['empty_option']);
114 if (isset($this->options
['disable_inarray_validator'])) {
115 $this->setDisableInArrayValidator($this->options
['disable_inarray_validator']);
122 * Set a single element attribute
125 * @param mixed $value
126 * @return Select|ElementInterface
128 public function setAttribute($key, $value)
130 // Do not include the options in the list of attributes
131 // TODO: Deprecate this
132 if ($key === 'options') {
133 $this->setValueOptions($value);
136 return parent
::setAttribute($key, $value);
140 * Set the flag to allow for disabling the automatic addition of an InArray validator.
142 * @param bool $disableOption
145 public function setDisableInArrayValidator($disableOption)
147 $this->disableInArrayValidator
= (bool) $disableOption;
152 * Get the disable in array validator flag.
156 public function disableInArrayValidator()
158 return $this->disableInArrayValidator
;
162 * Set the string for an empty option (can be empty string). If set to null, no option will be added
164 * @param string|null $emptyOption
167 public function setEmptyOption($emptyOption)
169 $this->emptyOption
= $emptyOption;
174 * Return the string for the empty option (null if none)
176 * @return string|null
178 public function getEmptyOption()
180 return $this->emptyOption
;
186 * @return \Zend\Validator\ValidatorInterface
188 protected function getValidator()
190 if (null === $this->validator
&& !$this->disableInArrayValidator()) {
191 $validator = new InArrayValidator(array(
192 'haystack' => $this->getValueOptionsValues(),
196 $multiple = (isset($this->attributes
['multiple']))
197 ?
$this->attributes
['multiple'] : null;
199 if (true === $multiple ||
'multiple' === $multiple) {
200 $validator = new ExplodeValidator(array(
201 'validator' => $validator,
202 'valueDelimiter' => null, // skip explode if only one value
206 $this->validator
= $validator;
208 return $this->validator
;
212 * Provide default input rules for this element
214 * Attaches the captcha as a validator.
218 public function getInputSpecification()
221 'name' => $this->getName(),
225 if ($validator = $this->getValidator()) {
226 $spec['validators'] = array(
235 * Get only the values from the options attribute
239 protected function getValueOptionsValues()
242 $options = $this->getValueOptions();
243 foreach ($options as $key => $optionSpec) {
244 if (is_array($optionSpec) && array_key_exists('options', $optionSpec)) {
245 foreach ($optionSpec['options'] as $nestedKey => $nestedOptionSpec) {
246 $values[] = $this->getOptionValue($nestedKey, $nestedOptionSpec);
251 $values[] = $this->getOptionValue($key, $optionSpec);
256 protected function getOptionValue($key, $optionSpec)
258 return is_array($optionSpec) ?
$optionSpec['value'] : $key;