12 * obtain it through the world-wide-web, please send an email |
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. |
13 * to license@zend.com so we can send you a copy immediately. |
14 * |
14 * |
15 * @category Zend |
15 * @category Zend |
16 * @package Zend_Form |
16 * @package Zend_Form |
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
17 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
19 */ |
19 */ |
20 |
20 |
21 /** @see Zend_Validate_Interface */ |
21 /** @see Zend_Validate_Interface */ |
22 require_once 'Zend/Validate/Interface.php'; |
22 require_once 'Zend/Validate/Interface.php'; |
24 /** |
24 /** |
25 * Zend_Form |
25 * Zend_Form |
26 * |
26 * |
27 * @category Zend |
27 * @category Zend |
28 * @package Zend_Form |
28 * @package Zend_Form |
29 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
29 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
30 * @license http://framework.zend.com/license/new-bsd New BSD License |
30 * @license http://framework.zend.com/license/new-bsd New BSD License |
31 * @version $Id: Form.php 23429 2010-11-22 23:06:46Z bittarman $ |
31 * @version $Id: Form.php 25223 2013-01-17 14:44:54Z frosch $ |
32 */ |
32 */ |
33 class Zend_Form implements Iterator, Countable, Zend_Validate_Interface |
33 class Zend_Form implements Iterator, Countable, Zend_Validate_Interface |
34 { |
34 { |
35 /**#@+ |
35 /**#@+ |
36 * Plugin loader type constants |
36 * Plugin loader type constants |
349 } |
349 } |
350 |
350 |
351 if (isset($options['attribs'])) { |
351 if (isset($options['attribs'])) { |
352 $this->addAttribs($options['attribs']); |
352 $this->addAttribs($options['attribs']); |
353 unset($options['attribs']); |
353 unset($options['attribs']); |
|
354 } |
|
355 |
|
356 if (isset($options['subForms'])) { |
|
357 $this->addSubForms($options['subForms']); |
|
358 unset($options['subForms']); |
354 } |
359 } |
355 |
360 |
356 $forbidden = array( |
361 $forbidden = array( |
357 'Options', 'Config', 'PluginLoader', 'SubForms', 'Translator', |
362 'Options', 'Config', 'PluginLoader', 'SubForms', 'Translator', |
358 'Attrib', 'Default', |
363 'Attrib', 'Default', |
493 case self::ELEMENT: |
498 case self::ELEMENT: |
494 $loader = $this->getPluginLoader($type); |
499 $loader = $this->getPluginLoader($type); |
495 $loader->addPrefixPath($prefix, $path); |
500 $loader->addPrefixPath($prefix, $path); |
496 return $this; |
501 return $this; |
497 case null: |
502 case null: |
498 $prefix = rtrim($prefix, '_'); |
503 $nsSeparator = (false !== strpos($prefix, '\\'))?'\\':'_'; |
|
504 $prefix = rtrim($prefix, $nsSeparator); |
499 $path = rtrim($path, DIRECTORY_SEPARATOR); |
505 $path = rtrim($path, DIRECTORY_SEPARATOR); |
500 foreach (array(self::DECORATOR, self::ELEMENT) as $type) { |
506 foreach (array(self::DECORATOR, self::ELEMENT) as $type) { |
501 $cType = ucfirst(strtolower($type)); |
507 $cType = ucfirst(strtolower($type)); |
502 $pluginPath = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR; |
508 $pluginPath = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR; |
503 $pluginPrefix = $prefix . '_' . $cType; |
509 $pluginPrefix = $prefix . $nsSeparator . $cType; |
504 $loader = $this->getPluginLoader($type); |
510 $loader = $this->getPluginLoader($type); |
505 $loader->addPrefixPath($pluginPrefix, $pluginPath); |
511 $loader->addPrefixPath($pluginPrefix, $pluginPath); |
506 } |
512 } |
507 return $this; |
513 return $this; |
508 default: |
514 default: |
1011 * and any provided $options will be ignored. |
1017 * and any provided $options will be ignored. |
1012 * |
1018 * |
1013 * @param string|Zend_Form_Element $element |
1019 * @param string|Zend_Form_Element $element |
1014 * @param string $name |
1020 * @param string $name |
1015 * @param array|Zend_Config $options |
1021 * @param array|Zend_Config $options |
|
1022 * @throws Zend_Form_Exception on invalid element |
1016 * @return Zend_Form |
1023 * @return Zend_Form |
1017 */ |
1024 */ |
1018 public function addElement($element, $name = null, $options = null) |
1025 public function addElement($element, $name = null, $options = null) |
1019 { |
1026 { |
1020 if (is_string($element)) { |
1027 if (is_string($element)) { |
1021 if (null === $name) { |
1028 if (null === $name) { |
1022 require_once 'Zend/Form/Exception.php'; |
1029 require_once 'Zend/Form/Exception.php'; |
1023 throw new Zend_Form_Exception('Elements specified by string must have an accompanying name'); |
1030 throw new Zend_Form_Exception( |
1024 } |
1031 'Elements specified by string must have an accompanying name' |
1025 |
1032 ); |
1026 if (is_array($this->_elementDecorators)) { |
|
1027 if (null === $options) { |
|
1028 $options = array('decorators' => $this->_elementDecorators); |
|
1029 } elseif ($options instanceof Zend_Config) { |
|
1030 $options = $options->toArray(); |
|
1031 } |
|
1032 if (is_array($options) |
|
1033 && !array_key_exists('decorators', $options) |
|
1034 ) { |
|
1035 $options['decorators'] = $this->_elementDecorators; |
|
1036 } |
|
1037 } |
1033 } |
1038 |
1034 |
1039 $this->_elements[$name] = $this->createElement($element, $name, $options); |
1035 $this->_elements[$name] = $this->createElement($element, $name, $options); |
1040 } elseif ($element instanceof Zend_Form_Element) { |
1036 } elseif ($element instanceof Zend_Form_Element) { |
1041 $prefixPaths = array(); |
1037 $prefixPaths = array(); |
1042 $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths(); |
1038 $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths(); |
1043 if (!empty($this->_elementPrefixPaths)) { |
1039 if (!empty($this->_elementPrefixPaths)) { |
1044 $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths); |
1040 $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths); |
1045 } |
1041 } |
1046 |
1042 |
|
1043 if (is_array($this->_elementDecorators) |
|
1044 && 0 == count($element->getDecorators()) |
|
1045 ) { |
|
1046 $element->setDecorators($this->_elementDecorators); |
|
1047 } |
|
1048 |
1047 if (null === $name) { |
1049 if (null === $name) { |
1048 $name = $element->getName(); |
1050 $name = $element->getName(); |
1049 } |
1051 } |
1050 |
1052 |
1051 $this->_elements[$name] = $element; |
1053 $this->_elements[$name] = $element; |
1052 $this->_elements[$name]->addPrefixPaths($prefixPaths); |
1054 $this->_elements[$name]->addPrefixPaths($prefixPaths); |
|
1055 } else { |
|
1056 require_once 'Zend/Form/Exception.php'; |
|
1057 throw new Zend_Form_Exception( |
|
1058 'Element must be specified by string or Zend_Form_Element instance' |
|
1059 ); |
1053 } |
1060 } |
1054 |
1061 |
1055 $this->_order[$name] = $this->_elements[$name]->getOrder(); |
1062 $this->_order[$name] = $this->_elements[$name]->getOrder(); |
1056 $this->_orderUpdated = true; |
1063 $this->_orderUpdated = true; |
1057 $this->_setElementsBelongTo($name); |
1064 $this->_setElementsBelongTo($name); |
1094 $options = $options->toArray(); |
1101 $options = $options->toArray(); |
1095 } |
1102 } |
1096 |
1103 |
1097 if ((null === $options) || !is_array($options)) { |
1104 if ((null === $options) || !is_array($options)) { |
1098 $options = array('prefixPath' => $prefixPaths); |
1105 $options = array('prefixPath' => $prefixPaths); |
|
1106 |
|
1107 if (is_array($this->_elementDecorators)) { |
|
1108 $options['decorators'] = $this->_elementDecorators; |
|
1109 } |
1099 } elseif (is_array($options)) { |
1110 } elseif (is_array($options)) { |
1100 if (array_key_exists('prefixPath', $options)) { |
1111 if (array_key_exists('prefixPath', $options)) { |
1101 $options['prefixPath'] = array_merge($prefixPaths, $options['prefixPath']); |
1112 $options['prefixPath'] = array_merge($prefixPaths, $options['prefixPath']); |
1102 } else { |
1113 } else { |
1103 $options['prefixPath'] = $prefixPaths; |
1114 $options['prefixPath'] = $prefixPaths; |
|
1115 } |
|
1116 |
|
1117 if (is_array($this->_elementDecorators) |
|
1118 && !array_key_exists('decorators', $options) |
|
1119 ) { |
|
1120 $options['decorators'] = $this->_elementDecorators; |
1104 } |
1121 } |
1105 } |
1122 } |
1106 |
1123 |
1107 $class = $this->getPluginLoader(self::ELEMENT)->load($type); |
1124 $class = $this->getPluginLoader(self::ELEMENT)->load($type); |
1108 $element = new $class($name, $options); |
1125 $element = new $class($name, $options); |
1338 */ |
1355 */ |
1339 public function getValues($suppressArrayNotation = false) |
1356 public function getValues($suppressArrayNotation = false) |
1340 { |
1357 { |
1341 $values = array(); |
1358 $values = array(); |
1342 $eBelongTo = null; |
1359 $eBelongTo = null; |
1343 |
1360 |
1344 if ($this->isArray()) { |
1361 if ($this->isArray()) { |
1345 $eBelongTo = $this->getElementsBelongTo(); |
1362 $eBelongTo = $this->getElementsBelongTo(); |
1346 } |
1363 } |
1347 |
1364 |
1348 foreach ($this->getElements() as $key => $element) { |
1365 foreach ($this->getElements() as $key => $element) { |
1349 if (!$element->getIgnore()) { |
1366 if (!$element->getIgnore()) { |
1350 $merge = array(); |
1367 $merge = array(); |
1351 if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) { |
1368 if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) { |
1352 if ('' !== (string)$belongsTo) { |
1369 if ('' !== (string)$belongsTo) { |
1784 public function addDisplayGroup(array $elements, $name, $options = null) |
1801 public function addDisplayGroup(array $elements, $name, $options = null) |
1785 { |
1802 { |
1786 $group = array(); |
1803 $group = array(); |
1787 foreach ($elements as $element) { |
1804 foreach ($elements as $element) { |
1788 if($element instanceof Zend_Form_Element) { |
1805 if($element instanceof Zend_Form_Element) { |
1789 $element = $element->getId(); |
1806 $elementName = $element->getName(); |
|
1807 if (!isset($this->_elements[$elementName])) { |
|
1808 $this->addElement($element); |
|
1809 } |
|
1810 $element = $elementName; |
1790 } |
1811 } |
1791 |
1812 |
1792 if (isset($this->_elements[$element])) { |
1813 if (isset($this->_elements[$element])) { |
1793 $add = $this->getElement($element); |
1814 $add = $this->getElement($element); |
1794 if (null !== $add) { |
1815 if (null !== $add) { |
2100 |
2121 |
2101 /** |
2122 /** |
2102 * Given an array, an optional arrayPath and a key this method |
2123 * Given an array, an optional arrayPath and a key this method |
2103 * dissolves the arrayPath and unsets the key within the array |
2124 * dissolves the arrayPath and unsets the key within the array |
2104 * if it exists. |
2125 * if it exists. |
2105 * |
2126 * |
2106 * @param array $array |
2127 * @param array $array |
2107 * @param string|null $arrayPath |
2128 * @param string|null $arrayPath |
2108 * @param string $key |
2129 * @param string $key |
2109 * @return array |
2130 * @return array |
2110 */ |
2131 */ |
2111 protected function _dissolveArrayUnsetKey($array, $arrayPath, $key) |
2132 protected function _dissolveArrayUnsetKey($array, $arrayPath, $key) |
2112 { |
2133 { |
2113 $unset =& $array; |
2134 $unset =& $array; |
2114 $path = trim(strtr((string)$arrayPath, array('[' => '/', ']' => '')), '/'); |
2135 $path = trim(strtr((string)$arrayPath, array('[' => '/', ']' => '')), '/'); |
2115 $segs = ('' !== $path) ? explode('/', $path) : array(); |
2136 $segs = ('' !== $path) ? explode('/', $path) : array(); |
2116 |
2137 |
2117 foreach ($segs as $seg) { |
2138 foreach ($segs as $seg) { |
2118 if (!array_key_exists($seg, (array)$unset)) { |
2139 if (!array_key_exists($seg, (array)$unset)) { |
2119 return $array; |
2140 return $array; |
2120 } |
2141 } |
2121 $unset =& $unset[$seg]; |
2142 $unset =& $unset[$seg]; |
2157 * Elements, SubForms and Elements from DisplayGroups as Values. |
2178 * Elements, SubForms and Elements from DisplayGroups as Values. |
2158 * |
2179 * |
2159 * Subitems are inserted based on their order Setting if set, |
2180 * Subitems are inserted based on their order Setting if set, |
2160 * otherwise they are appended, the resulting numerical index |
2181 * otherwise they are appended, the resulting numerical index |
2161 * may differ from the order value. |
2182 * may differ from the order value. |
2162 * |
2183 * |
2163 * @access protected |
2184 * @access protected |
2164 * @return array |
2185 * @return array |
2165 */ |
2186 */ |
2166 public function getElementsAndSubFormsOrdered() |
2187 public function getElementsAndSubFormsOrdered() |
2167 { |
2188 { |
2168 $ordered = array(); |
2189 $ordered = array(); |
2169 foreach ($this->_order as $name => $order) { |
2190 foreach ($this->_order as $name => $order) { |
2248 $valid = $element->isValid($check[$key], $context) && $valid; |
2269 $valid = $element->isValid($check[$key], $context) && $valid; |
2249 $data = $this->_dissolveArrayUnsetKey($data, $belongsTo, $key); |
2270 $data = $this->_dissolveArrayUnsetKey($data, $belongsTo, $key); |
2250 } |
2271 } |
2251 } |
2272 } |
2252 foreach ($this->getSubForms() as $key => $form) { |
2273 foreach ($this->getSubForms() as $key => $form) { |
2253 if (null !== $translator && !$form->hasTranslator()) { |
2274 if (null !== $translator && $this->hasTranslator() |
|
2275 && !$form->hasTranslator()) { |
2254 $form->setTranslator($translator); |
2276 $form->setTranslator($translator); |
2255 } |
2277 } |
2256 if (isset($data[$key]) && !$form->isArray()) { |
2278 if (isset($data[$key]) && !$form->isArray()) { |
2257 $valid = $form->isValid($data[$key]) && $valid; |
2279 $valid = $form->isValid($data[$key]) && $valid; |
2258 } else { |
2280 } else { |
2478 return $this->getElement($name)->getErrors(); |
2511 return $this->getElement($name)->getErrors(); |
2479 } else if (isset($this->_subForms[$name])) { |
2512 } else if (isset($this->_subForms[$name])) { |
2480 return $this->getSubForm($name)->getErrors(null, true); |
2513 return $this->getSubForm($name)->getErrors(null, true); |
2481 } |
2514 } |
2482 } |
2515 } |
2483 |
2516 |
2484 foreach ($this->_elements as $key => $element) { |
2517 foreach ($this->_elements as $key => $element) { |
2485 $errors[$key] = $element->getErrors(); |
2518 $errors[$key] = $element->getErrors(); |
2486 } |
2519 } |
2487 foreach ($this->getSubForms() as $key => $subForm) { |
2520 foreach ($this->getSubForms() as $key => $subForm) { |
2488 $merge = array(); |
2521 $merge = array(); |