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.
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';
26 * Base class for multi-option form elements
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
35 abstract class Zend_Form_Element_Multi
extends Zend_Form_Element_Xhtml
38 * Array of options for multi-item
41 public $options = array();
44 * Flag: autoregister inArray validator?
47 protected $_registerInArrayValidator = true;
50 * Separator to use between options; defaults to '<br />'.
53 protected $_separator = '<br />';
56 * Which values are translated already?
59 protected $_translated = array();
66 public function getSeparator()
68 return $this->_separator
;
74 * @param mixed $separator
77 public function setSeparator($separator)
79 $this->_separator
= $separator;
84 * Retrieve options array
88 protected function _getMultiOptions()
90 if (null === $this->options ||
!is_array($this->options
)) {
91 $this->options
= array();
94 return $this->options
;
100 * @param string $option
101 * @param string $value
102 * @return Zend_Form_Element_Multi
104 public function addMultiOption($option, $value = '')
106 $option = (string) $option;
107 $this->_getMultiOptions();
108 if (!$this->_translateOption($option, $value)) {
109 $this->options
[$option] = $value;
116 * Add many options at once
118 * @param array $options
119 * @return Zend_Form_Element_Multi
121 public function addMultiOptions(array $options)
123 foreach ($options as $option => $value) {
125 && array_key_exists('key', $value)
126 && array_key_exists('value', $value)
128 $this->addMultiOption($value['key'], $value['value']);
130 $this->addMultiOption($option, $value);
137 * Set all options at once (overwrites)
139 * @param array $options
140 * @return Zend_Form_Element_Multi
142 public function setMultiOptions(array $options)
144 $this->clearMultiOptions();
145 return $this->addMultiOptions($options);
149 * Retrieve single multi option
151 * @param string $option
154 public function getMultiOption($option)
156 $option = (string) $option;
157 $this->_getMultiOptions();
158 if (isset($this->options
[$option])) {
159 $this->_translateOption($option, $this->options
[$option]);
160 return $this->options
[$option];
171 public function getMultiOptions()
173 $this->_getMultiOptions();
174 foreach ($this->options
as $option => $value) {
175 $this->_translateOption($option, $value);
177 return $this->options
;
181 * Remove a single multi option
183 * @param string $option
186 public function removeMultiOption($option)
188 $option = (string) $option;
189 $this->_getMultiOptions();
190 if (isset($this->options
[$option])) {
191 unset($this->options
[$option]);
192 if (isset($this->_translated
[$option])) {
193 unset($this->_translated
[$option]);
204 * @return Zend_Form_Element_Multi
206 public function clearMultiOptions()
208 $this->options
= array();
209 $this->_translated
= array();
214 * Set flag indicating whether or not to auto-register inArray validator
217 * @return Zend_Form_Element_Multi
219 public function setRegisterInArrayValidator($flag)
221 $this->_registerInArrayValidator
= (bool) $flag;
226 * Get status of auto-register inArray validator flag
230 public function registerInArrayValidator()
232 return $this->_registerInArrayValidator
;
236 * Is the value provided valid?
238 * Autoregisters InArray validator if necessary.
240 * @param string $value
241 * @param mixed $context
244 public function isValid($value, $context = null)
246 if ($this->registerInArrayValidator()) {
247 if (!$this->getValidator('InArray')) {
248 $multiOptions = $this->getMultiOptions();
251 foreach ($multiOptions as $opt_value => $opt_label) {
252 // optgroup instead of option label
253 if (is_array($opt_label)) {
254 $options = array_merge($options, array_keys($opt_label));
257 $options[] = $opt_value;
268 return parent
::isValid($value, $context);
272 * Translate an option
274 * @param string $option
275 * @param string $value
278 protected function _translateOption($option, $value)
280 if ($this->translatorIsDisabled()) {
284 if (!isset($this->_translated
[$option]) && !empty($value)) {
285 $this->options
[$option] = $this->_translateValue($value);
286 if ($this->options
[$option] === $value) {
289 $this->_translated
[$option] = true;
297 * Translate a multi option value
299 * @param string $value
302 protected function _translateValue($value)
304 if (is_array($value)) {
305 foreach ($value as $key => $val) {
306 $value[$key] = $this->_translateValue($val);
310 if (null !== ($translator = $this->getTranslator())) {
311 return $translator->translate($value);