PageRenderTime 62ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/Form.php

https://bitbucket.org/winponta/zend
PHP | 3426 lines | 1987 code | 343 blank | 1096 comment | 325 complexity | 8f5d4ae9bcee02fdc4c09eb022d4616b MD5 | raw file

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

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