PageRenderTime 59ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Zend/Form.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 3393 lines | 1939 code | 339 blank | 1115 comment | 319 complexity | ff642b7e89e20242614e35b58e64bdd0 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Form
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** @see Zend_Validate_Interface */
  21. #require_once 'Zend/Validate/Interface.php';
  22. /**
  23. * Zend_Form
  24. *
  25. * @category Zend
  26. * @package Zend_Form
  27. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. * @version $Id: Form.php 23429 2010-11-22 23:06:46Z bittarman $
  30. */
  31. class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
  32. {
  33. /**#@+
  34. * Plugin loader type constants
  35. */
  36. const DECORATOR = 'DECORATOR';
  37. const ELEMENT = 'ELEMENT';
  38. /**#@-*/
  39. /**#@+
  40. * Method type constants
  41. */
  42. const METHOD_DELETE = 'delete';
  43. const METHOD_GET = 'get';
  44. const METHOD_POST = 'post';
  45. const METHOD_PUT = 'put';
  46. /**#@-*/
  47. /**#@+
  48. * Encoding type constants
  49. */
  50. const ENCTYPE_URLENCODED = 'application/x-www-form-urlencoded';
  51. const ENCTYPE_MULTIPART = 'multipart/form-data';
  52. /**#@-*/
  53. /**
  54. * Form metadata and attributes
  55. * @var array
  56. */
  57. protected $_attribs = array();
  58. /**
  59. * Decorators for rendering
  60. * @var array
  61. */
  62. protected $_decorators = array();
  63. /**
  64. * Default display group class
  65. * @var string
  66. */
  67. protected $_defaultDisplayGroupClass = 'Zend_Form_DisplayGroup';
  68. /**
  69. * Form description
  70. * @var string
  71. */
  72. protected $_description;
  73. /**
  74. * Should we disable loading the default decorators?
  75. * @var bool
  76. */
  77. protected $_disableLoadDefaultDecorators = false;
  78. /**
  79. * Display group prefix paths
  80. * @var array
  81. */
  82. protected $_displayGroupPrefixPaths = array();
  83. /**
  84. * Groups of elements grouped for display purposes
  85. * @var array
  86. */
  87. protected $_displayGroups = array();
  88. /**
  89. * Global decorators to apply to all elements
  90. * @var null|array
  91. */
  92. protected $_elementDecorators;
  93. /**
  94. * Prefix paths to use when creating elements
  95. * @var array
  96. */
  97. protected $_elementPrefixPaths = array();
  98. /**
  99. * Form elements
  100. * @var array
  101. */
  102. protected $_elements = array();
  103. /**
  104. * Array to which elements belong (if any)
  105. * @var string
  106. */
  107. protected $_elementsBelongTo;
  108. /**
  109. * Custom form-level error messages
  110. * @var array
  111. */
  112. protected $_errorMessages = array();
  113. /**
  114. * Are there errors in the form?
  115. * @var bool
  116. */
  117. protected $_errorsExist = false;
  118. /**
  119. * Has the form been manually flagged as an error?
  120. * @var bool
  121. */
  122. protected $_errorsForced = false;
  123. /**
  124. * Form order
  125. * @var int|null
  126. */
  127. protected $_formOrder;
  128. /**
  129. * Whether or not form elements are members of an array
  130. * @var bool
  131. */
  132. protected $_isArray = false;
  133. /**
  134. * Form legend
  135. * @var string
  136. */
  137. protected $_legend;
  138. /**
  139. * Plugin loaders
  140. * @var array
  141. */
  142. protected $_loaders = array();
  143. /**
  144. * Allowed form methods
  145. * @var array
  146. */
  147. protected $_methods = array('delete', 'get', 'post', 'put');
  148. /**
  149. * Order in which to display and iterate elements
  150. * @var array
  151. */
  152. protected $_order = array();
  153. /**
  154. * Whether internal order has been updated or not
  155. * @var bool
  156. */
  157. protected $_orderUpdated = false;
  158. /**
  159. * Sub form prefix paths
  160. * @var array
  161. */
  162. protected $_subFormPrefixPaths = array();
  163. /**
  164. * Sub forms
  165. * @var array
  166. */
  167. protected $_subForms = array();
  168. /**
  169. * @var Zend_Translate
  170. */
  171. protected $_translator;
  172. /**
  173. * Global default translation adapter
  174. * @var Zend_Translate
  175. */
  176. protected static $_translatorDefault;
  177. /**
  178. * is the translator disabled?
  179. * @var bool
  180. */
  181. protected $_translatorDisabled = false;
  182. /**
  183. * @var Zend_View_Interface
  184. */
  185. protected $_view;
  186. /**
  187. * @var bool
  188. */
  189. protected $_isRendered = false;
  190. /**
  191. * Constructor
  192. *
  193. * Registers form view helper as decorator
  194. *
  195. * @param mixed $options
  196. * @return void
  197. */
  198. public function __construct($options = null)
  199. {
  200. if (is_array($options)) {
  201. $this->setOptions($options);
  202. } elseif ($options instanceof Zend_Config) {
  203. $this->setConfig($options);
  204. }
  205. // Extensions...
  206. $this->init();
  207. $this->loadDefaultDecorators();
  208. }
  209. /**
  210. * Clone form object and all children
  211. *
  212. * @return void
  213. */
  214. public function __clone()
  215. {
  216. $elements = array();
  217. foreach ($this->getElements() as $name => $element) {
  218. $elements[] = clone $element;
  219. }
  220. $this->setElements($elements);
  221. $subForms = array();
  222. foreach ($this->getSubForms() as $name => $subForm) {
  223. $subForms[$name] = clone $subForm;
  224. }
  225. $this->setSubForms($subForms);
  226. $displayGroups = array();
  227. foreach ($this->_displayGroups as $group) {
  228. $clone = clone $group;
  229. $elements = array();
  230. foreach ($clone->getElements() as $name => $e) {
  231. $elements[] = $this->getElement($name);
  232. }
  233. $clone->setElements($elements);
  234. $displayGroups[] = $clone;
  235. }
  236. $this->setDisplayGroups($displayGroups);
  237. }
  238. /**
  239. * Reset values of form
  240. *
  241. * @return Zend_Form
  242. */
  243. public function reset()
  244. {
  245. foreach ($this->getElements() as $element) {
  246. $element->setValue(null);
  247. }
  248. foreach ($this->getSubForms() as $subForm) {
  249. $subForm->reset();
  250. }
  251. return $this;
  252. }
  253. /**
  254. * Initialize form (used by extending classes)
  255. *
  256. * @return void
  257. */
  258. public function init()
  259. {
  260. }
  261. /**
  262. * Set form state from options array
  263. *
  264. * @param array $options
  265. * @return Zend_Form
  266. */
  267. public function setOptions(array $options)
  268. {
  269. if (isset($options['prefixPath'])) {
  270. $this->addPrefixPaths($options['prefixPath']);
  271. unset($options['prefixPath']);
  272. }
  273. if (isset($options['elementPrefixPath'])) {
  274. $this->addElementPrefixPaths($options['elementPrefixPath']);
  275. unset($options['elementPrefixPath']);
  276. }
  277. if (isset($options['displayGroupPrefixPath'])) {
  278. $this->addDisplayGroupPrefixPaths($options['displayGroupPrefixPath']);
  279. unset($options['displayGroupPrefixPath']);
  280. }
  281. if (isset($options['elementDecorators'])) {
  282. $this->_elementDecorators = $options['elementDecorators'];
  283. unset($options['elementDecorators']);
  284. }
  285. if (isset($options['elements'])) {
  286. $this->setElements($options['elements']);
  287. unset($options['elements']);
  288. }
  289. if (isset($options['defaultDisplayGroupClass'])) {
  290. $this->setDefaultDisplayGroupClass($options['defaultDisplayGroupClass']);
  291. unset($options['defaultDisplayGroupClass']);
  292. }
  293. if (isset($options['displayGroupDecorators'])) {
  294. $displayGroupDecorators = $options['displayGroupDecorators'];
  295. unset($options['displayGroupDecorators']);
  296. }
  297. if (isset($options['elementsBelongTo'])) {
  298. $elementsBelongTo = $options['elementsBelongTo'];
  299. unset($options['elementsBelongTo']);
  300. }
  301. if (isset($options['attribs'])) {
  302. $this->addAttribs($options['attribs']);
  303. unset($options['attribs']);
  304. }
  305. $forbidden = array(
  306. 'Options', 'Config', 'PluginLoader', 'SubForms', 'Translator',
  307. 'Attrib', 'Default',
  308. );
  309. foreach ($options as $key => $value) {
  310. $normalized = ucfirst($key);
  311. if (in_array($normalized, $forbidden)) {
  312. continue;
  313. }
  314. $method = 'set' . $normalized;
  315. if (method_exists($this, $method)) {
  316. if($normalized == 'View' && !($value instanceof Zend_View_Interface)) {
  317. continue;
  318. }
  319. $this->$method($value);
  320. } else {
  321. $this->setAttrib($key, $value);
  322. }
  323. }
  324. if (isset($displayGroupDecorators)) {
  325. $this->setDisplayGroupDecorators($displayGroupDecorators);
  326. }
  327. if (isset($elementsBelongTo)) {
  328. $this->setElementsBelongTo($elementsBelongTo);
  329. }
  330. return $this;
  331. }
  332. /**
  333. * Set form state from config object
  334. *
  335. * @param Zend_Config $config
  336. * @return Zend_Form
  337. */
  338. public function setConfig(Zend_Config $config)
  339. {
  340. return $this->setOptions($config->toArray());
  341. }
  342. // Loaders
  343. /**
  344. * Set plugin loaders for use with decorators and elements
  345. *
  346. * @param Zend_Loader_PluginLoader_Interface $loader
  347. * @param string $type 'decorator' or 'element'
  348. * @return Zend_Form
  349. * @throws Zend_Form_Exception on invalid type
  350. */
  351. public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type = null)
  352. {
  353. $type = strtoupper($type);
  354. switch ($type) {
  355. case self::DECORATOR:
  356. case self::ELEMENT:
  357. $this->_loaders[$type] = $loader;
  358. return $this;
  359. default:
  360. #require_once 'Zend/Form/Exception.php';
  361. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type));
  362. }
  363. }
  364. /**
  365. * Retrieve plugin loader for given type
  366. *
  367. * $type may be one of:
  368. * - decorator
  369. * - element
  370. *
  371. * If a plugin loader does not exist for the given type, defaults are
  372. * created.
  373. *
  374. * @param string $type
  375. * @return Zend_Loader_PluginLoader_Interface
  376. */
  377. public function getPluginLoader($type = null)
  378. {
  379. $type = strtoupper($type);
  380. if (!isset($this->_loaders[$type])) {
  381. switch ($type) {
  382. case self::DECORATOR:
  383. $prefixSegment = 'Form_Decorator';
  384. $pathSegment = 'Form/Decorator';
  385. break;
  386. case self::ELEMENT:
  387. $prefixSegment = 'Form_Element';
  388. $pathSegment = 'Form/Element';
  389. break;
  390. default:
  391. #require_once 'Zend/Form/Exception.php';
  392. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  393. }
  394. #require_once 'Zend/Loader/PluginLoader.php';
  395. $this->_loaders[$type] = new Zend_Loader_PluginLoader(
  396. array('Zend_' . $prefixSegment . '_' => 'Zend/' . $pathSegment . '/')
  397. );
  398. }
  399. return $this->_loaders[$type];
  400. }
  401. /**
  402. * Add prefix path for plugin loader
  403. *
  404. * If no $type specified, assumes it is a base path for both filters and
  405. * validators, and sets each according to the following rules:
  406. * - decorators: $prefix = $prefix . '_Decorator'
  407. * - elements: $prefix = $prefix . '_Element'
  408. *
  409. * Otherwise, the path prefix is set on the appropriate plugin loader.
  410. *
  411. * If $type is 'decorator', sets the path in the decorator plugin loader
  412. * for all elements. Additionally, if no $type is provided,
  413. * the prefix and path is added to both decorator and element
  414. * plugin loader with following settings:
  415. * $prefix . '_Decorator', $path . '/Decorator/'
  416. * $prefix . '_Element', $path . '/Element/'
  417. *
  418. * @param string $prefix
  419. * @param string $path
  420. * @param string $type
  421. * @return Zend_Form
  422. * @throws Zend_Form_Exception for invalid type
  423. */
  424. public function addPrefixPath($prefix, $path, $type = null)
  425. {
  426. $type = strtoupper($type);
  427. switch ($type) {
  428. case self::DECORATOR:
  429. case self::ELEMENT:
  430. $loader = $this->getPluginLoader($type);
  431. $loader->addPrefixPath($prefix, $path);
  432. return $this;
  433. case null:
  434. $prefix = rtrim($prefix, '_');
  435. $path = rtrim($path, DIRECTORY_SEPARATOR);
  436. foreach (array(self::DECORATOR, self::ELEMENT) as $type) {
  437. $cType = ucfirst(strtolower($type));
  438. $pluginPath = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR;
  439. $pluginPrefix = $prefix . '_' . $cType;
  440. $loader = $this->getPluginLoader($type);
  441. $loader->addPrefixPath($pluginPrefix, $pluginPath);
  442. }
  443. return $this;
  444. default:
  445. #require_once 'Zend/Form/Exception.php';
  446. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  447. }
  448. }
  449. /**
  450. * Add many prefix paths at once
  451. *
  452. * @param array $spec
  453. * @return Zend_Form
  454. */
  455. public function addPrefixPaths(array $spec)
  456. {
  457. if (isset($spec['prefix']) && isset($spec['path'])) {
  458. return $this->addPrefixPath($spec['prefix'], $spec['path']);
  459. }
  460. foreach ($spec as $type => $paths) {
  461. if (is_numeric($type) && is_array($paths)) {
  462. $type = null;
  463. if (isset($paths['prefix']) && isset($paths['path'])) {
  464. if (isset($paths['type'])) {
  465. $type = $paths['type'];
  466. }
  467. $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
  468. }
  469. } elseif (!is_numeric($type)) {
  470. if (!isset($paths['prefix']) || !isset($paths['path'])) {
  471. continue;
  472. }
  473. $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
  474. }
  475. }
  476. return $this;
  477. }
  478. /**
  479. * Add prefix path for all elements
  480. *
  481. * @param string $prefix
  482. * @param string $path
  483. * @param string $type
  484. * @return Zend_Form
  485. */
  486. public function addElementPrefixPath($prefix, $path, $type = null)
  487. {
  488. $this->_elementPrefixPaths[] = array(
  489. 'prefix' => $prefix,
  490. 'path' => $path,
  491. 'type' => $type,
  492. );
  493. foreach ($this->getElements() as $element) {
  494. $element->addPrefixPath($prefix, $path, $type);
  495. }
  496. foreach ($this->getSubForms() as $subForm) {
  497. $subForm->addElementPrefixPath($prefix, $path, $type);
  498. }
  499. return $this;
  500. }
  501. /**
  502. * Add prefix paths for all elements
  503. *
  504. * @param array $spec
  505. * @return Zend_Form
  506. */
  507. public function addElementPrefixPaths(array $spec)
  508. {
  509. $this->_elementPrefixPaths = $this->_elementPrefixPaths + $spec;
  510. foreach ($this->getElements() as $element) {
  511. $element->addPrefixPaths($spec);
  512. }
  513. return $this;
  514. }
  515. /**
  516. * Add prefix path for all display groups
  517. *
  518. * @param string $prefix
  519. * @param string $path
  520. * @return Zend_Form
  521. */
  522. public function addDisplayGroupPrefixPath($prefix, $path)
  523. {
  524. $this->_displayGroupPrefixPaths[] = array(
  525. 'prefix' => $prefix,
  526. 'path' => $path,
  527. );
  528. foreach ($this->getDisplayGroups() as $group) {
  529. $group->addPrefixPath($prefix, $path);
  530. }
  531. return $this;
  532. }
  533. /**
  534. * Add multiple display group prefix paths at once
  535. *
  536. * @param array $spec
  537. * @return Zend_Form
  538. */
  539. public function addDisplayGroupPrefixPaths(array $spec)
  540. {
  541. foreach ($spec as $key => $value) {
  542. if (is_string($value) && !is_numeric($key)) {
  543. $this->addDisplayGroupPrefixPath($key, $value);
  544. continue;
  545. }
  546. if (is_string($value) && is_numeric($key)) {
  547. continue;
  548. }
  549. if (is_array($value)) {
  550. $count = count($value);
  551. if (array_keys($value) === range(0, $count - 1)) {
  552. if ($count < 2) {
  553. continue;
  554. }
  555. $prefix = array_shift($value);
  556. $path = array_shift($value);
  557. $this->addDisplayGroupPrefixPath($prefix, $path);
  558. continue;
  559. }
  560. if (array_key_exists('prefix', $value) && array_key_exists('path', $value)) {
  561. $this->addDisplayGroupPrefixPath($value['prefix'], $value['path']);
  562. }
  563. }
  564. }
  565. return $this;
  566. }
  567. // Form metadata:
  568. /**
  569. * Set form attribute
  570. *
  571. * @param string $key
  572. * @param mixed $value
  573. * @return Zend_Form
  574. */
  575. public function setAttrib($key, $value)
  576. {
  577. $key = (string) $key;
  578. $this->_attribs[$key] = $value;
  579. return $this;
  580. }
  581. /**
  582. * Add multiple form attributes at once
  583. *
  584. * @param array $attribs
  585. * @return Zend_Form
  586. */
  587. public function addAttribs(array $attribs)
  588. {
  589. foreach ($attribs as $key => $value) {
  590. $this->setAttrib($key, $value);
  591. }
  592. return $this;
  593. }
  594. /**
  595. * Set multiple form attributes at once
  596. *
  597. * Overwrites any previously set attributes.
  598. *
  599. * @param array $attribs
  600. * @return Zend_Form
  601. */
  602. public function setAttribs(array $attribs)
  603. {
  604. $this->clearAttribs();
  605. return $this->addAttribs($attribs);
  606. }
  607. /**
  608. * Retrieve a single form attribute
  609. *
  610. * @param string $key
  611. * @return mixed
  612. */
  613. public function getAttrib($key)
  614. {
  615. $key = (string) $key;
  616. if (!isset($this->_attribs[$key])) {
  617. return null;
  618. }
  619. return $this->_attribs[$key];
  620. }
  621. /**
  622. * Retrieve all form attributes/metadata
  623. *
  624. * @return array
  625. */
  626. public function getAttribs()
  627. {
  628. return $this->_attribs;
  629. }
  630. /**
  631. * Remove attribute
  632. *
  633. * @param string $key
  634. * @return bool
  635. */
  636. public function removeAttrib($key)
  637. {
  638. if (isset($this->_attribs[$key])) {
  639. unset($this->_attribs[$key]);
  640. return true;
  641. }
  642. return false;
  643. }
  644. /**
  645. * Clear all form attributes
  646. *
  647. * @return Zend_Form
  648. */
  649. public function clearAttribs()
  650. {
  651. $this->_attribs = array();
  652. return $this;
  653. }
  654. /**
  655. * Set form action
  656. *
  657. * @param string $action
  658. * @return Zend_Form
  659. */
  660. public function setAction($action)
  661. {
  662. return $this->setAttrib('action', (string) $action);
  663. }
  664. /**
  665. * Get form action
  666. *
  667. * Sets default to '' if not set.
  668. *
  669. * @return string
  670. */
  671. public function getAction()
  672. {
  673. $action = $this->getAttrib('action');
  674. if (null === $action) {
  675. $action = '';
  676. $this->setAction($action);
  677. }
  678. return $action;
  679. }
  680. /**
  681. * Set form method
  682. *
  683. * Only values in {@link $_methods()} allowed
  684. *
  685. * @param string $method
  686. * @return Zend_Form
  687. * @throws Zend_Form_Exception
  688. */
  689. public function setMethod($method)
  690. {
  691. $method = strtolower($method);
  692. if (!in_array($method, $this->_methods)) {
  693. #require_once 'Zend/Form/Exception.php';
  694. throw new Zend_Form_Exception(sprintf('"%s" is an invalid form method', $method));
  695. }
  696. $this->setAttrib('method', $method);
  697. return $this;
  698. }
  699. /**
  700. * Retrieve form method
  701. *
  702. * @return string
  703. */
  704. public function getMethod()
  705. {
  706. if (null === ($method = $this->getAttrib('method'))) {
  707. $method = self::METHOD_POST;
  708. $this->setAttrib('method', $method);
  709. }
  710. return strtolower($method);
  711. }
  712. /**
  713. * Set encoding type
  714. *
  715. * @param string $value
  716. * @return Zend_Form
  717. */
  718. public function setEnctype($value)
  719. {
  720. $this->setAttrib('enctype', $value);
  721. return $this;
  722. }
  723. /**
  724. * Get encoding type
  725. *
  726. * @return string
  727. */
  728. public function getEnctype()
  729. {
  730. if (null === ($enctype = $this->getAttrib('enctype'))) {
  731. $enctype = self::ENCTYPE_URLENCODED;
  732. $this->setAttrib('enctype', $enctype);
  733. }
  734. return $this->getAttrib('enctype');
  735. }
  736. /**
  737. * Filter a name to only allow valid variable characters
  738. *
  739. * @param string $value
  740. * @param bool $allowBrackets
  741. * @return string
  742. */
  743. public function filterName($value, $allowBrackets = false)
  744. {
  745. $charset = '^a-zA-Z0-9_\x7f-\xff';
  746. if ($allowBrackets) {
  747. $charset .= '\[\]';
  748. }
  749. return preg_replace('/[' . $charset . ']/', '', (string) $value);
  750. }
  751. /**
  752. * Set form name
  753. *
  754. * @param string $name
  755. * @return Zend_Form
  756. */
  757. public function setName($name)
  758. {
  759. $name = $this->filterName($name);
  760. if ('' === (string)$name) {
  761. #require_once 'Zend/Form/Exception.php';
  762. throw new Zend_Form_Exception('Invalid name provided; must contain only valid variable characters and be non-empty');
  763. }
  764. return $this->setAttrib('name', $name);
  765. }
  766. /**
  767. * Get name attribute
  768. *
  769. * @return null|string
  770. */
  771. public function getName()
  772. {
  773. return $this->getAttrib('name');
  774. }
  775. /**
  776. * Get fully qualified name
  777. *
  778. * Places name as subitem of array and/or appends brackets.
  779. *
  780. * @return string
  781. */
  782. public function getFullyQualifiedName()
  783. {
  784. return $this->getName();
  785. }
  786. /**
  787. * Get element id
  788. *
  789. * @return string
  790. */
  791. public function getId()
  792. {
  793. if (null !== ($id = $this->getAttrib('id'))) {
  794. return $id;
  795. }
  796. $id = $this->getFullyQualifiedName();
  797. // Bail early if no array notation detected
  798. if (!strstr($id, '[')) {
  799. return $id;
  800. }
  801. // Strip array notation
  802. if ('[]' == substr($id, -2)) {
  803. $id = substr($id, 0, strlen($id) - 2);
  804. }
  805. $id = str_replace('][', '-', $id);
  806. $id = str_replace(array(']', '['), '-', $id);
  807. $id = trim($id, '-');
  808. return $id;
  809. }
  810. /**
  811. * Set form legend
  812. *
  813. * @param string $value
  814. * @return Zend_Form
  815. */
  816. public function setLegend($value)
  817. {
  818. $this->_legend = (string) $value;
  819. return $this;
  820. }
  821. /**
  822. * Get form legend
  823. *
  824. * @return string
  825. */
  826. public function getLegend()
  827. {
  828. return $this->_legend;
  829. }
  830. /**
  831. * Set form description
  832. *
  833. * @param string $value
  834. * @return Zend_Form
  835. */
  836. public function setDescription($value)
  837. {
  838. $this->_description = (string) $value;
  839. return $this;
  840. }
  841. /**
  842. * Retrieve form description
  843. *
  844. * @return string
  845. */
  846. public function getDescription()
  847. {
  848. return $this->_description;
  849. }
  850. /**
  851. * Set form order
  852. *
  853. * @param int $index
  854. * @return Zend_Form
  855. */
  856. public function setOrder($index)
  857. {
  858. $this->_formOrder = (int) $index;
  859. return $this;
  860. }
  861. /**
  862. * Get form order
  863. *
  864. * @return int|null
  865. */
  866. public function getOrder()
  867. {
  868. return $this->_formOrder;
  869. }
  870. /**
  871. * When calling renderFormElements or render this method
  872. * is used to set $_isRendered member to prevent repeatedly
  873. * merging belongsTo setting
  874. */
  875. protected function _setIsRendered()
  876. {
  877. $this->_isRendered = true;
  878. return $this;
  879. }
  880. /**
  881. * Get the value of $_isRendered member
  882. */
  883. protected function _getIsRendered()
  884. {
  885. return (bool)$this->_isRendered;
  886. }
  887. // Element interaction:
  888. /**
  889. * Add a new element
  890. *
  891. * $element may be either a string element type, or an object of type
  892. * Zend_Form_Element. If a string element type is provided, $name must be
  893. * provided, and $options may be optionally provided for configuring the
  894. * element.
  895. *
  896. * If a Zend_Form_Element is provided, $name may be optionally provided,
  897. * and any provided $options will be ignored.
  898. *
  899. * @param string|Zend_Form_Element $element
  900. * @param string $name
  901. * @param array|Zend_Config $options
  902. * @return Zend_Form
  903. */
  904. public function addElement($element, $name = null, $options = null)
  905. {
  906. if (is_string($element)) {
  907. if (null === $name) {
  908. #require_once 'Zend/Form/Exception.php';
  909. throw new Zend_Form_Exception('Elements specified by string must have an accompanying name');
  910. }
  911. if (is_array($this->_elementDecorators)) {
  912. if (null === $options) {
  913. $options = array('decorators' => $this->_elementDecorators);
  914. } elseif ($options instanceof Zend_Config) {
  915. $options = $options->toArray();
  916. }
  917. if (is_array($options)
  918. && !array_key_exists('decorators', $options)
  919. ) {
  920. $options['decorators'] = $this->_elementDecorators;
  921. }
  922. }
  923. $this->_elements[$name] = $this->createElement($element, $name, $options);
  924. } elseif ($element instanceof Zend_Form_Element) {
  925. $prefixPaths = array();
  926. $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
  927. if (!empty($this->_elementPrefixPaths)) {
  928. $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
  929. }
  930. if (null === $name) {
  931. $name = $element->getName();
  932. }
  933. $this->_elements[$name] = $element;
  934. $this->_elements[$name]->addPrefixPaths($prefixPaths);
  935. }
  936. $this->_order[$name] = $this->_elements[$name]->getOrder();
  937. $this->_orderUpdated = true;
  938. $this->_setElementsBelongTo($name);
  939. return $this;
  940. }
  941. /**
  942. * Create an element
  943. *
  944. * Acts as a factory for creating elements. Elements created with this
  945. * method will not be attached to the form, but will contain element
  946. * settings as specified in the form object (including plugin loader
  947. * prefix paths, default decorators, etc.).
  948. *
  949. * @param string $type
  950. * @param string $name
  951. * @param array|Zend_Config $options
  952. * @return Zend_Form_Element
  953. */
  954. public function createElement($type, $name, $options = null)
  955. {
  956. if (!is_string($type)) {
  957. #require_once 'Zend/Form/Exception.php';
  958. throw new Zend_Form_Exception('Element type must be a string indicating type');
  959. }
  960. if (!is_string($name)) {
  961. #require_once 'Zend/Form/Exception.php';
  962. throw new Zend_Form_Exception('Element name must be a string');
  963. }
  964. $prefixPaths = array();
  965. $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
  966. if (!empty($this->_elementPrefixPaths)) {
  967. $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
  968. }
  969. if ($options instanceof Zend_Config) {
  970. $options = $options->toArray();
  971. }
  972. if ((null === $options) || !is_array($options)) {
  973. $options = array('prefixPath' => $prefixPaths);
  974. } elseif (is_array($options)) {
  975. if (array_key_exists('prefixPath', $options)) {
  976. $options['prefixPath'] = array_merge($prefixPaths, $options['prefixPath']);
  977. } else {
  978. $options['prefixPath'] = $prefixPaths;
  979. }
  980. }
  981. $class = $this->getPluginLoader(self::ELEMENT)->load($type);
  982. $element = new $class($name, $options);
  983. return $element;
  984. }
  985. /**
  986. * Add multiple elements at once
  987. *
  988. * @param array $elements
  989. * @return Zend_Form
  990. */
  991. public function addElements(array $elements)
  992. {
  993. foreach ($elements as $key => $spec) {
  994. $name = null;
  995. if (!is_numeric($key)) {
  996. $name = $key;
  997. }
  998. if (is_string($spec) || ($spec instanceof Zend_Form_Element)) {
  999. $this->addElement($spec, $name);
  1000. continue;
  1001. }
  1002. if (is_array($spec)) {
  1003. $argc = count($spec);
  1004. $options = array();
  1005. if (isset($spec['type'])) {
  1006. $type = $spec['type'];
  1007. if (isset($spec['name'])) {
  1008. $name = $spec['name'];
  1009. }
  1010. if (isset($spec['options'])) {
  1011. $options = $spec['options'];
  1012. }
  1013. $this->addElement($type, $name, $options);
  1014. } else {
  1015. switch ($argc) {
  1016. case 0:
  1017. continue;
  1018. case (1 <= $argc):
  1019. $type = array_shift($spec);
  1020. case (2 <= $argc):
  1021. if (null === $name) {
  1022. $name = array_shift($spec);
  1023. } else {
  1024. $options = array_shift($spec);
  1025. }
  1026. case (3 <= $argc):
  1027. if (empty($options)) {
  1028. $options = array_shift($spec);
  1029. }
  1030. default:
  1031. $this->addElement($type, $name, $options);
  1032. }
  1033. }
  1034. }
  1035. }
  1036. return $this;
  1037. }
  1038. /**
  1039. * Set form elements (overwrites existing elements)
  1040. *
  1041. * @param array $elements
  1042. * @return Zend_Form
  1043. */
  1044. public function setElements(array $elements)
  1045. {
  1046. $this->clearElements();
  1047. return $this->addElements($elements);
  1048. }
  1049. /**
  1050. * Retrieve a single element
  1051. *
  1052. * @param string $name
  1053. * @return Zend_Form_Element|null
  1054. */
  1055. public function getElement($name)
  1056. {
  1057. if (array_key_exists($name, $this->_elements)) {
  1058. return $this->_elements[$name];
  1059. }
  1060. return null;
  1061. }
  1062. /**
  1063. * Retrieve all elements
  1064. *
  1065. * @return array
  1066. */
  1067. public function getElements()
  1068. {
  1069. return $this->_elements;
  1070. }
  1071. /**
  1072. * Remove element
  1073. *
  1074. * @param string $name
  1075. * @return boolean
  1076. */
  1077. public function removeElement($name)
  1078. {
  1079. $name = (string) $name;
  1080. if (isset($this->_elements[$name])) {
  1081. unset($this->_elements[$name]);
  1082. if (array_key_exists($name, $this->_order)) {
  1083. unset($this->_order[$name]);
  1084. $this->_orderUpdated = true;
  1085. } else {
  1086. foreach ($this->_displayGroups as $group) {
  1087. if (null !== $group->getElement($name)) {
  1088. $group->removeElement($name);
  1089. }
  1090. }
  1091. }
  1092. return true;
  1093. }
  1094. return false;
  1095. }
  1096. /**
  1097. * Remove all form elements
  1098. *
  1099. * @return Zend_Form
  1100. */
  1101. public function clearElements()
  1102. {
  1103. foreach (array_keys($this->_elements) as $key) {
  1104. if (array_key_exists($key, $this->_order)) {
  1105. unset($this->_order[$key]);
  1106. }
  1107. }
  1108. $this->_elements = array();
  1109. $this->_orderUpdated = true;
  1110. return $this;
  1111. }
  1112. /**
  1113. * Set default values for elements
  1114. *
  1115. * Sets values for all elements specified in the array of $defaults.
  1116. *
  1117. * @param array $defaults
  1118. * @return Zend_Form
  1119. */
  1120. public function setDefaults(array $defaults)
  1121. {
  1122. $eBelongTo = null;
  1123. if ($this->isArray()) {
  1124. $eBelongTo = $this->getElementsBelongTo();
  1125. $defaults = $this->_dissolveArrayValue($defaults, $eBelongTo);
  1126. }
  1127. foreach ($this->getElements() as $name => $element) {
  1128. $check = $defaults;
  1129. if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
  1130. $check = $this->_dissolveArrayValue($defaults, $belongsTo);
  1131. }
  1132. if (array_key_exists($name, $check)) {
  1133. $this->setDefault($name, $check[$name]);
  1134. $defaults = $this->_dissolveArrayUnsetKey($defaults, $belongsTo, $name);
  1135. }
  1136. }
  1137. foreach ($this->getSubForms() as $name => $form) {
  1138. if (!$form->isArray() && array_key_exists($name, $defaults)) {
  1139. $form->setDefaults($defaults[$name]);
  1140. } else {
  1141. $form->setDefaults($defaults);
  1142. }
  1143. }
  1144. return $this;
  1145. }
  1146. /**
  1147. * Set default value for an element
  1148. *
  1149. * @param string $name
  1150. * @param mixed $value
  1151. * @return Zend_Form
  1152. */
  1153. public function setDefault($name, $value)
  1154. {
  1155. $name = (string) $name;
  1156. if ($element = $this->getElement($name)) {
  1157. $element->setValue($value);
  1158. } else {
  1159. if (is_scalar($value)) {
  1160. foreach ($this->getSubForms() as $subForm) {
  1161. $subForm->setDefault($name, $value);
  1162. }
  1163. } elseif (is_array($value) && ($subForm = $this->getSubForm($name))) {
  1164. $subForm->setDefaults($value);
  1165. }
  1166. }
  1167. return $this;
  1168. }
  1169. /**
  1170. * Retrieve value for single element
  1171. *
  1172. * @param string $name
  1173. * @return mixed
  1174. */
  1175. public function getValue($name)
  1176. {
  1177. if ($element = $this->getElement($name)) {
  1178. return $element->getValue();
  1179. }
  1180. if ($subForm = $this->getSubForm($name)) {
  1181. return $subForm->getValues(true);
  1182. }
  1183. foreach ($this->getSubForms() as $subForm) {
  1184. if ($name == $subForm->getElementsBelongTo()) {
  1185. return $subForm->getValues(true);
  1186. }
  1187. }
  1188. return null;
  1189. }
  1190. /**
  1191. * Retrieve all form element values
  1192. *
  1193. * @param bool $suppressArrayNotation
  1194. * @return array
  1195. */
  1196. public function getValues($suppressArrayNotation = false)
  1197. {
  1198. $values = array();
  1199. $eBelongTo = null;
  1200. if ($this->isArray()) {
  1201. $eBelongTo = $this->getElementsBelongTo();
  1202. }
  1203. foreach ($this->getElements() as $key => $element) {
  1204. if (!$element->getIgnore()) {
  1205. $merge = array();
  1206. if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
  1207. if ('' !== (string)$belongsTo) {
  1208. $key = $belongsTo . '[' . $key . ']';
  1209. }
  1210. }
  1211. $merge = $this->_attachToArray($element->getValue(), $key);
  1212. $values = $this->_array_replace_recursive($values, $merge);
  1213. }
  1214. }
  1215. foreach ($this->getSubForms() as $key => $subForm) {
  1216. $merge = array();
  1217. if (!$subForm->isArray()) {
  1218. $merge[$key] = $subForm->getValues();
  1219. } else {
  1220. $merge = $this->_attachToArray($subForm->getValues(true),
  1221. $subForm->getElementsBelongTo());
  1222. }
  1223. $values = $this->_array_replace_recursive($values, $merge);
  1224. }
  1225. if (!$suppressArrayNotation &&
  1226. $this->isArray() &&
  1227. !$this->_getIsRendered()) {
  1228. $values = $this->_attachToArray($values, $this->getElementsBelongTo());
  1229. }
  1230. return $values;
  1231. }
  1232. /**
  1233. * Returns only the valid values from the given form input.
  1234. *
  1235. * For models that can be saved in a partially valid state, for example when following the builder,
  1236. * prototype or state patterns it is particularly interessting to retrieve all the current valid
  1237. * values to persist them.
  1238. *
  1239. * @param array $data
  1240. * @param bool $suppressArrayNotation
  1241. * @return array
  1242. */
  1243. public function getValidValues($data, $suppressArrayNotation = false)
  1244. {
  1245. $values = array();
  1246. $eBelongTo = null;
  1247. if ($this->isArray()) {
  1248. $eBelongTo = $this->getElementsBelongTo();
  1249. $data = $this->_dissolveArrayValue($data, $eBelongTo);
  1250. }
  1251. $context = $data;
  1252. foreach ($this->getElements() as $key => $element) {
  1253. if (!$element->getIgnore()) {
  1254. $check = $data;
  1255. if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
  1256. $check = $this->_dissolveArrayValue($data, $belongsTo);
  1257. }
  1258. if (isset($check[$key])) {
  1259. if($element->isValid($check[$key], $context)) {
  1260. $merge = array();
  1261. if ($belongsTo !== $eBelongTo && '' !== (string)$belongsTo) {
  1262. $key = $belongsTo . '[' . $key . ']';
  1263. }
  1264. $merge = $this->_attachToArray($element->getValue(), $key);
  1265. $values = $this->_array_replace_recursive($values, $merge);
  1266. }
  1267. $data = $this->_dissolveArrayUnsetKey($data, $belongsTo, $key);
  1268. }
  1269. }
  1270. }
  1271. foreach ($this->getSubForms() as $key => $form) {
  1272. $merge = array();
  1273. if (isset($data[$key]) && !$form->isArray()) {
  1274. $tmp = $form->getValidValues($data[$key]);
  1275. if (!empty($tmp)) {
  1276. $merge[$key] = $tmp;
  1277. }
  1278. } else {
  1279. $tmp = $form->getValidValues($data, true);
  1280. if (!empty($tmp)) {
  1281. $merge = $this->_attachToArray($tmp, $form->getElementsBelongTo());
  1282. }
  1283. }
  1284. $values = $this->_array_replace_recursive($values, $merge);
  1285. }
  1286. if (!$suppressArrayNotation &&
  1287. $this->isArray() &&
  1288. !empty($values) &&
  1289. !$this->_getIsRendered()) {
  1290. $values = $this->_attachToArray($values, $this->getElementsBelongTo());
  1291. }
  1292. return $values;
  1293. }
  1294. /**
  1295. * Get unfiltered element value
  1296. *
  1297. * @param string $name
  1298. * @return mixed
  1299. */
  1300. public function getUnfilteredValue($name)
  1301. {
  1302. if ($element = $this->getElement($name)) {
  1303. return $element->getUnfilteredValue();
  1304. }
  1305. return null;
  1306. }
  1307. /**
  1308. * Retrieve all unfiltered element values
  1309. *
  1310. * @return array
  1311. */
  1312. public function getUnfilteredValues()
  1313. {
  1314. $values = array();
  1315. foreach ($this->getElements() as $key => $element) {
  1316. $values[$key] = $element->getUnfilteredValue();
  1317. }
  1318. return $values;
  1319. }
  1320. /**
  1321. * Set all elements' filters
  1322. *
  1323. * @param array $filters
  1324. * @return Zend_Form
  1325. */
  1326. public function setElementFilters(array $filters)
  1327. {
  1328. foreach ($this->getElements() as $element) {
  1329. $element->setFilters($filters);
  1330. }
  1331. return $this;
  1332. }
  1333. /**
  1334. * Set name of array elements belong to
  1335. *
  1336. * @param string $array
  1337. * @return Zend_Form
  1338. */
  1339. public function setElementsBelongTo($array)
  1340. {
  1341. $origName = $this->getElementsBelongTo();
  1342. $name = $this->filterName($array, true);
  1343. if ('' === $name) {
  1344. $name = null;
  1345. }
  1346. $this->_elementsBelongTo = $name;
  1347. if (null === $name) {
  1348. $this->setIsArray(false);
  1349. if (null !== $origName) {
  1350. $this->_setElementsBelongTo();
  1351. }
  1352. } else {
  1353. $this->setIsArray(true);
  1354. $this->_setElementsBelongTo();
  1355. }
  1356. return $this;
  1357. }
  1358. /**
  1359. * Set array to which elements belong
  1360. *
  1361. * @param string $name Element name
  1362. * @return void
  1363. */
  1364. protected function _setElementsBelongTo($name = null)
  1365. {
  1366. $array = $this->getElementsBelongTo();
  1367. if (null === $array) {
  1368. return;
  1369. }
  1370. if (null === $name) {
  1371. foreach ($this->getElements() as $element) {
  1372. $element->setBelongsTo($array);
  1373. }
  1374. } else {
  1375. if (null !== ($element = $this->getElement($name))) {
  1376. $element->setBelongsTo($array);
  1377. }
  1378. }
  1379. }
  1380. /**
  1381. * Get name of array elements belong to
  1382. *
  1383. * @return string|null
  1384. */
  1385. public function getElementsBelongTo()
  1386. {
  1387. if ((null === $this->_elementsBelongTo) && $this->isArray()) {
  1388. $name = $this->getName();
  1389. if ('' !== (string)$name) {
  1390. return $name;
  1391. }
  1392. }
  1393. return $this->_elementsBelongTo;
  1394. }
  1395. /**
  1396. * Set flag indicating elements belong to array
  1397. *
  1398. * @param bool $flag Value of flag
  1399. * @return Zend_Form
  1400. */
  1401. public function setIsArray($flag)
  1402. {
  1403. $this->_isArray = (bool) $flag;
  1404. return $this;
  1405. }
  1406. /**
  1407. * Get flag indicating if elements belong to an array
  1408. *
  1409. * @return bool
  1410. */
  1411. public function isArray()
  1412. {
  1413. return $this->_isArray;
  1414. }
  1415. // Element groups:
  1416. /**
  1417. * Add a form group/subform
  1418. *
  1419. * @param Zend_Form $form
  1420. * @param string $name
  1421. * @param int $order
  1422. * @return Zend_Form
  1423. */
  1424. public function addSubForm(Zend_Form $form, $name, $order = null)
  1425. {
  1426. $name = (string) $name;
  1427. foreach ($this->_loaders as $type => $loader) {
  1428. $loaderPaths = $loader->getPaths();
  1429. foreach ($loaderPaths as $prefix => $paths) {
  1430. foreach ($paths as $path) {
  1431. $form->addPrefixPath($prefix, $path, $type);
  1432. }
  1433. }
  1434. }
  1435. if (!empty($this->_elementPrefixPaths)) {
  1436. foreach ($this->_elementPrefixPaths as $spec) {
  1437. list($prefix, $path, $type) = array_values($spec);
  1438. $form->addElementPrefixPath($prefix, $path, $type);
  1439. }
  1440. }
  1441. if (!empty($this->_displayGroupPrefixPaths)) {
  1442. foreach ($this->_displayGroupPrefixPaths as $spec) {
  1443. list($prefix, $path) = array_values($spec);
  1444. $form->addDisplayGroupPrefixPath($prefix, $path);
  1445. }
  1446. }
  1447. if (null !== $order) {
  1448. $form->setOrder($order);
  1449. }
  1450. if (($oldName = $form->getName()) &&
  1451. $oldName !== $name &&
  1452. $oldName === $form->getElementsBelongTo()) {
  1453. $form->setElementsBelongTo($name);
  1454. }
  1455. $form->setName($name);
  1456. $this->_subForms[$name] = $form;
  1457. $this->_order[$name] = $order;
  1458. $this->_orderUpdated = true;
  1459. return $this;
  1460. }
  1461. /**
  1462. * Add multiple form subForms/subforms at once
  1463. *
  1464. * @param array $subForms
  1465. * @return Zend_Form
  1466. */
  1467. public function addSubForms(array $subForms)
  1468. {
  1469. foreach ($subForms as $key => $spec) {
  1470. $name = null;
  1471. if (!is_numeric($key)) {
  1472. $name = $key;
  1473. }
  1474. if ($spec instanceof Zend_Form) {
  1475. $this->addSubForm($spec, $name);
  1476. continue;
  1477. }
  1478. if (is_array($spec)) {
  1479. $argc = count($spec);
  1480. $order = null;
  1481. switch ($argc) {
  1482. case 0:
  1483. continue;
  1484. case (1 <= $argc):
  1485. $subForm = array_shift($spec);
  1486. case (2 <= $argc):
  1487. $name = array_shift($spec);
  1488. case (3 <= $argc):
  1489. $order = array_shift($spec);
  1490. default:
  1491. $this->addSubForm($subForm, $name, $order);
  1492. }
  1493. }
  1494. }
  1495. return $this;
  1496. }
  1497. /**
  1498. * Set multiple form subForms/subforms (overwrites)
  1499. *
  1500. * @param array $subForms
  1501. * @return Zend_Form
  1502. */
  1503. public function setSubForms(array $subForms)
  1504. {
  1505. $this->clearSubForms();
  1506. return $this->addSubForms($subForms);
  1507. }
  1508. /**
  1509. * Retrieve a form subForm/subform
  1510. *
  1511. * @param string $name
  1512. * @return Zend_Form|null
  1513. */
  1514. public function getSubForm($name)
  1515. {
  1516. $name = (string) $name;
  1517. if (isset($this->_subForms[$name])) {
  1518. return $this->_subForms[$name];
  1519. }
  1520. return null;
  1521. }
  1522. /**
  1523. * Retrieve all form subForms/subforms
  1524. *
  1525. * @return array
  1526. */
  1527. public function getSubForms()
  1528. {
  1529. return $this->_subForms;
  1530. }
  1531. /**
  1532. * Remove form subForm/subform
  1533. *
  1534. * @param string $name
  1535. * @return boolean
  1536. */
  1537. public function removeSubForm($name)
  1538. {
  1539. $name = (string) $name;
  1540. if (array_key_exists($name, $this->_subForms)) {
  1541. unset($this->_subForms[$name]);
  1542. if (array_key_exists($name, $this->_order)) {
  1543. unset($this->_order[$name]);
  1544. $this->_orderUpdated = true;
  1545. }
  1546. return true;
  1547. }
  1548. return false;
  1549. }
  1550. /**
  1551. * Remove all form subForms/subforms
  1552. *
  1553. * @return Zend_Form
  1554. */
  1555. public function clearSubForms()
  1556. {
  1557. foreach (array_keys($this->_subForms) as $key) {
  1558. if (array_key_exists($key, $this->_order)) {
  1559. unset($this->_order[$key]);
  1560. }
  1561. }
  1562. $this->_subForms = array();
  1563. $this->_orderUpdated = true;
  1564. return $this;
  1565. }
  1566. // Display groups:
  1567. /**
  1568. * Set default display group class
  1569. *
  1570. * @param string $class
  1571. * @return Zend_Form
  1572. */
  1573. public function setDefaultDisplayGroupClass($class)
  1574. {
  1575. $this->_defaultDisplayGroupClass = (string) $class;
  1576. return $this;
  1577. }
  1578. /**
  1579. * Retrieve default display group class
  1580. *
  1581. * @return string
  1582. */
  1583. public function getDefaultDisplayGroupClass()
  1584. {
  1585. return $this->_defaultDisplayGroupClass;
  1586. }
  1587. /**
  1588. * Add a display group
  1589. *
  1590. * Groups named elements for display purposes.
  1591. *
  1592. * If a referenced element does not yet exist in the form, it is omitted.
  1593. *
  1594. * @param array $elements
  1595. * @param string $name
  1596. * @param array|Zend_Config $options
  1597. * @return Zend_Form
  1598. * @throws Zend_Form_Exception if no valid elements provided
  1599. */
  1600. public function addDisplayGroup(array $elements, $name, $options = null)
  1601. {
  1602. $group = array();
  1603. foreach ($elements as $element) {
  1604. if($element instanceof Zend_Form_Element) {
  1605. $element = $element->getId();
  1606. }
  1607. if (isset($this->_elements[$element])) {
  1608. $add = $this->getElement($element);
  1609. if (null !== $add) {
  1610. $group[] = $add;
  1611. }
  1612. }
  1613. }
  1614. if (empty($group)) {
  1615. #require_once 'Zend/Form/Exception.php';
  1616. throw new Zend_Form_Exception('No valid elements specified for display group');
  1617. }
  1618. $name = (string) $name;
  1619. if (is_array($options)) {
  1620. $options['form'] = $this;
  1621. $options['elements'] = $group;
  1622. } elseif ($options instanceof Zend_Config) {
  1623. $options = $options->toArray();
  1624. $options['form'] = $this;
  1625. $options['elements'] = $group;
  1626. } else {
  1627. $options = array(
  1628. 'form' => $this,
  1629. 'elements' => $group,
  1630. );
  1631. }
  1632. if (isset($options['displayGroupClass'])) {
  1633. $class = $options['displayGroupClass'];
  1634. unset($options['displayGroupClass']);
  1635. } else {
  1636. $class = $this->getDefaultDisplayGroupClass();
  1637. }
  1638. if (!class_exists($class)) {
  1639. #require_once 'Zend/Loader.php';
  1640. Zend_Loader::loadClass($class);
  1641. }
  1642. $this->_displayGroups[$name] = new $class(
  1643. $name,
  1644. $this->getPluginLoader(self::DECORATOR),
  1645. $options
  1646. );
  1647. if (!empty($this->_displayGroupPrefixPaths)) {
  1648. $this->_displayGroups[$name]->addPrefixPaths($this->_displayGroupPrefixPaths);
  1649. }
  1650. $this->_order[$name] = $this->_displayGroups[$name]->getOrder();
  1651. $this->_orderUpdated = true;
  1652. return $this;
  1653. }
  1654. /**
  1655. * Add a display group object (used with cloning)
  1656. *
  1657. * @param Zend_Form_Disp…

Large files files are truncated, but you can click here to view the full file