PageRenderTime 54ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Form.php

https://bitbucket.org/imaweda/jelly2
PHP | 3401 lines | 1972 code | 339 blank | 1090 comment | 321 complexity | 856e4f984bb5b3413759fa042ba148f7 MD5 | raw file
Possible License(s): BSD-3-Clause

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-2011 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-2011 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 23950 2011-05-03 03:46:42Z ralph $
  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. * @throws Zend_Form_Exception on invalid element
  903. * @return Zend_Form
  904. */
  905. public function addElement($element, $name = null, $options = null)
  906. {
  907. if (is_string($element)) {
  908. if (null === $name) {
  909. require_once 'Zend/Form/Exception.php';
  910. throw new Zend_Form_Exception('Elements specified by string must have an accompanying name');
  911. }
  912. if (is_array($this->_elementDecorators)) {
  913. if (null === $options) {
  914. $options = array('decorators' => $this->_elementDecorators);
  915. } elseif ($options instanceof Zend_Config) {
  916. $options = $options->toArray();
  917. }
  918. if (is_array($options)
  919. && !array_key_exists('decorators', $options)
  920. ) {
  921. $options['decorators'] = $this->_elementDecorators;
  922. }
  923. }
  924. $this->_elements[$name] = $this->createElement($element, $name, $options);
  925. } elseif ($element instanceof Zend_Form_Element) {
  926. $prefixPaths = array();
  927. $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
  928. if (!empty($this->_elementPrefixPaths)) {
  929. $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
  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('Element must be specified by string or Zend_Form_Element instance');
  939. }
  940. $this->_order[$name] = $this->_elements[$name]->getOrder();
  941. $this->_orderUpdated = true;
  942. $this->_setElementsBelongTo($name);
  943. return $this;
  944. }
  945. /**
  946. * Create an element
  947. *
  948. * Acts as a factory for creating elements. Elements created with this
  949. * method will not be attached to the form, but will contain element
  950. * settings as specified in the form object (including plugin loader
  951. * prefix paths, default decorators, etc.).
  952. *
  953. * @param string $type
  954. * @param string $name
  955. * @param array|Zend_Config $options
  956. * @return Zend_Form_Element
  957. */
  958. public function createElement($type, $name, $options = null)
  959. {
  960. if (!is_string($type)) {
  961. require_once 'Zend/Form/Exception.php';
  962. throw new Zend_Form_Exception('Element type must be a string indicating type');
  963. }
  964. if (!is_string($name)) {
  965. require_once 'Zend/Form/Exception.php';
  966. throw new Zend_Form_Exception('Element name must be a string');
  967. }
  968. $prefixPaths = array();
  969. $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
  970. if (!empty($this->_elementPrefixPaths)) {
  971. $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
  972. }
  973. if ($options instanceof Zend_Config) {
  974. $options = $options->toArray();
  975. }
  976. if ((null === $options) || !is_array($options)) {
  977. $options = array('prefixPath' => $prefixPaths);
  978. } elseif (is_array($options)) {
  979. if (array_key_exists('prefixPath', $options)) {
  980. $options['prefixPath'] = array_merge($prefixPaths, $options['prefixPath']);
  981. } else {
  982. $options['prefixPath'] = $prefixPaths;
  983. }
  984. }
  985. $class = $this->getPluginLoader(self::ELEMENT)->load($type);
  986. $element = new $class($name, $options);
  987. return $element;
  988. }
  989. /**
  990. * Add multiple elements at once
  991. *
  992. * @param array $elements
  993. * @return Zend_Form
  994. */
  995. public function addElements(array $elements)
  996. {
  997. foreach ($elements as $key => $spec) {
  998. $name = null;
  999. if (!is_numeric($key)) {
  1000. $name = $key;
  1001. }
  1002. if (is_string($spec) || ($spec instanceof Zend_Form_Element)) {
  1003. $this->addElement($spec, $name);
  1004. continue;
  1005. }
  1006. if (is_array($spec)) {
  1007. $argc = count($spec);
  1008. $options = array();
  1009. if (isset($spec['type'])) {
  1010. $type = $spec['type'];
  1011. if (isset($spec['name'])) {
  1012. $name = $spec['name'];
  1013. }
  1014. if (isset($spec['options'])) {
  1015. $options = $spec['options'];
  1016. }
  1017. $this->addElement($type, $name, $options);
  1018. } else {
  1019. switch ($argc) {
  1020. case 0:
  1021. continue;
  1022. case (1 <= $argc):
  1023. $type = array_shift($spec);
  1024. case (2 <= $argc):
  1025. if (null === $name) {
  1026. $name = array_shift($spec);
  1027. } else {
  1028. $options = array_shift($spec);
  1029. }
  1030. case (3 <= $argc):
  1031. if (empty($options)) {
  1032. $options = array_shift($spec);
  1033. }
  1034. default:
  1035. $this->addElement($type, $name, $options);
  1036. }
  1037. }
  1038. }
  1039. }
  1040. return $this;
  1041. }
  1042. /**
  1043. * Set form elements (overwrites existing elements)
  1044. *
  1045. * @param array $elements
  1046. * @return Zend_Form
  1047. */
  1048. public function setElements(array $elements)
  1049. {
  1050. $this->clearElements();
  1051. return $this->addElements($elements);
  1052. }
  1053. /**
  1054. * Retrieve a single element
  1055. *
  1056. * @param string $name
  1057. * @return Zend_Form_Element|null
  1058. */
  1059. public function getElement($name)
  1060. {
  1061. if (array_key_exists($name, $this->_elements)) {
  1062. return $this->_elements[$name];
  1063. }
  1064. return null;
  1065. }
  1066. /**
  1067. * Retrieve all elements
  1068. *
  1069. * @return array
  1070. */
  1071. public function getElements()
  1072. {
  1073. return $this->_elements;
  1074. }
  1075. /**
  1076. * Remove element
  1077. *
  1078. * @param string $name
  1079. * @return boolean
  1080. */
  1081. public function removeElement($name)
  1082. {
  1083. $name = (string) $name;
  1084. if (isset($this->_elements[$name])) {
  1085. unset($this->_elements[$name]);
  1086. if (array_key_exists($name, $this->_order)) {
  1087. unset($this->_order[$name]);
  1088. $this->_orderUpdated = true;
  1089. } else {
  1090. foreach ($this->_displayGroups as $group) {
  1091. if (null !== $group->getElement($name)) {
  1092. $group->removeElement($name);
  1093. }
  1094. }
  1095. }
  1096. return true;
  1097. }
  1098. return false;
  1099. }
  1100. /**
  1101. * Remove all form elements
  1102. *
  1103. * @return Zend_Form
  1104. */
  1105. public function clearElements()
  1106. {
  1107. foreach (array_keys($this->_elements) as $key) {
  1108. if (array_key_exists($key, $this->_order)) {
  1109. unset($this->_order[$key]);
  1110. }
  1111. }
  1112. $this->_elements = array();
  1113. $this->_orderUpdated = true;
  1114. return $this;
  1115. }
  1116. /**
  1117. * Set default values for elements
  1118. *
  1119. * Sets values for all elements specified in the array of $defaults.
  1120. *
  1121. * @param array $defaults
  1122. * @return Zend_Form
  1123. */
  1124. public function setDefaults(array $defaults)
  1125. {
  1126. $eBelongTo = null;
  1127. if ($this->isArray()) {
  1128. $eBelongTo = $this->getElementsBelongTo();
  1129. $defaults = $this->_dissolveArrayValue($defaults, $eBelongTo);
  1130. }
  1131. foreach ($this->getElements() as $name => $element) {
  1132. $check = $defaults;
  1133. if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
  1134. $check = $this->_dissolveArrayValue($defaults, $belongsTo);
  1135. }
  1136. if (array_key_exists($name, $check)) {
  1137. $this->setDefault($name, $check[$name]);
  1138. $defaults = $this->_dissolveArrayUnsetKey($defaults, $belongsTo, $name);
  1139. }
  1140. }
  1141. foreach ($this->getSubForms() as $name => $form) {
  1142. if (!$form->isArray() && array_key_exists($name, $defaults)) {
  1143. $form->setDefaults($defaults[$name]);
  1144. } else {
  1145. $form->setDefaults($defaults);
  1146. }
  1147. }
  1148. return $this;
  1149. }
  1150. /**
  1151. * Set default value for an element
  1152. *
  1153. * @param string $name
  1154. * @param mixed $value
  1155. * @return Zend_Form
  1156. */
  1157. public function setDefault($name, $value)
  1158. {
  1159. $name = (string) $name;
  1160. if ($element = $this->getElement($name)) {
  1161. $element->setValue($value);
  1162. } else {
  1163. if (is_scalar($value)) {
  1164. foreach ($this->getSubForms() as $subForm) {
  1165. $subForm->setDefault($name, $value);
  1166. }
  1167. } elseif (is_array($value) && ($subForm = $this->getSubForm($name))) {
  1168. $subForm->setDefaults($value);
  1169. }
  1170. }
  1171. return $this;
  1172. }
  1173. /**
  1174. * Retrieve value for single element
  1175. *
  1176. * @param string $name
  1177. * @return mixed
  1178. */
  1179. public function getValue($name)
  1180. {
  1181. if ($element = $this->getElement($name)) {
  1182. return $element->getValue();
  1183. }
  1184. if ($subForm = $this->getSubForm($name)) {
  1185. return $subForm->getValues(true);
  1186. }
  1187. foreach ($this->getSubForms() as $subForm) {
  1188. if ($name == $subForm->getElementsBelongTo()) {
  1189. return $subForm->getValues(true);
  1190. }
  1191. }
  1192. return null;
  1193. }
  1194. /**
  1195. * Retrieve all form element values
  1196. *
  1197. * @param bool $suppressArrayNotation
  1198. * @return array
  1199. */
  1200. public function getValues($suppressArrayNotation = false)
  1201. {
  1202. $values = array();
  1203. $eBelongTo = null;
  1204. if ($this->isArray()) {
  1205. $eBelongTo = $this->getElementsBelongTo();
  1206. }
  1207. foreach ($this->getElements() as $key => $element) {
  1208. if (!$element->getIgnore()) {
  1209. $merge = array();
  1210. if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
  1211. if ('' !== (string)$belongsTo) {
  1212. $key = $belongsTo . '[' . $key . ']';
  1213. }
  1214. }
  1215. $merge = $this->_attachToArray($element->getValue(), $key);
  1216. $values = $this->_array_replace_recursive($values, $merge);
  1217. }
  1218. }
  1219. foreach ($this->getSubForms() as $key => $subForm) {
  1220. $merge = array();
  1221. if (!$subForm->isArray()) {
  1222. $merge[$key] = $subForm->getValues();
  1223. } else {
  1224. $merge = $this->_attachToArray($subForm->getValues(true),
  1225. $subForm->getElementsBelongTo());
  1226. }
  1227. $values = $this->_array_replace_recursive($values, $merge);
  1228. }
  1229. if (!$suppressArrayNotation &&
  1230. $this->isArray() &&
  1231. !$this->_getIsRendered()) {
  1232. $values = $this->_attachToArray($values, $this->getElementsBelongTo());
  1233. }
  1234. return $values;
  1235. }
  1236. /**
  1237. * Returns only the valid values from the given form input.
  1238. *
  1239. * For models that can be saved in a partially valid state, for example when following the builder,
  1240. * prototype or state patterns it is particularly interessting to retrieve all the current valid
  1241. * values to persist them.
  1242. *
  1243. * @param array $data
  1244. * @param bool $suppressArrayNotation
  1245. * @return array
  1246. */
  1247. public function getValidValues($data, $suppressArrayNotation = false)
  1248. {
  1249. $values = array();
  1250. $eBelongTo = null;
  1251. if ($this->isArray()) {
  1252. $eBelongTo = $this->getElementsBelongTo();
  1253. $data = $this->_dissolveArrayValue($data, $eBelongTo);
  1254. }
  1255. $context = $data;
  1256. foreach ($this->getElements() as $key => $element) {
  1257. if (!$element->getIgnore()) {
  1258. $check = $data;
  1259. if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
  1260. $check = $this->_dissolveArrayValue($data, $belongsTo);
  1261. }
  1262. if (isset($check[$key])) {
  1263. if($element->isValid($check[$key], $context)) {
  1264. $merge = array();
  1265. if ($belongsTo !== $eBelongTo && '' !== (string)$belongsTo) {
  1266. $key = $belongsTo . '[' . $key . ']';
  1267. }
  1268. $merge = $this->_attachToArray($element->getValue(), $key);
  1269. $values = $this->_array_replace_recursive($values, $merge);
  1270. }
  1271. $data = $this->_dissolveArrayUnsetKey($data, $belongsTo, $key);
  1272. }
  1273. }
  1274. }
  1275. foreach ($this->getSubForms() as $key => $form) {
  1276. $merge = array();
  1277. if (isset($data[$key]) && !$form->isArray()) {
  1278. $tmp = $form->getValidValues($data[$key]);
  1279. if (!empty($tmp)) {
  1280. $merge[$key] = $tmp;
  1281. }
  1282. } else {
  1283. $tmp = $form->getValidValues($data, true);
  1284. if (!empty($tmp)) {
  1285. $merge = $this->_attachToArray($tmp, $form->getElementsBelongTo());
  1286. }
  1287. }
  1288. $values = $this->_array_replace_recursive($values, $merge);
  1289. }
  1290. if (!$suppressArrayNotation &&
  1291. $this->isArray() &&
  1292. !empty($values) &&
  1293. !$this->_getIsRendered()) {
  1294. $values = $this->_attachToArray($values, $this->getElementsBelongTo());
  1295. }
  1296. return $values;
  1297. }
  1298. /**
  1299. * Get unfiltered element value
  1300. *
  1301. * @param string $name
  1302. * @return mixed
  1303. */
  1304. public function getUnfilteredValue($name)
  1305. {
  1306. if ($element = $this->getElement($name)) {
  1307. return $element->getUnfilteredValue();
  1308. }
  1309. return null;
  1310. }
  1311. /**
  1312. * Retrieve all unfiltered element values
  1313. *
  1314. * @return array
  1315. */
  1316. public function getUnfilteredValues()
  1317. {
  1318. $values = array();
  1319. foreach ($this->getElements() as $key => $element) {
  1320. $values[$key] = $element->getUnfilteredValue();
  1321. }
  1322. return $values;
  1323. }
  1324. /**
  1325. * Set all elements' filters
  1326. *
  1327. * @param array $filters
  1328. * @return Zend_Form
  1329. */
  1330. public function setElementFilters(array $filters)
  1331. {
  1332. foreach ($this->getElements() as $element) {
  1333. $element->setFilters($filters);
  1334. }
  1335. return $this;
  1336. }
  1337. /**
  1338. * Set name of array elements belong to
  1339. *
  1340. * @param string $array
  1341. * @return Zend_Form
  1342. */
  1343. public function setElementsBelongTo($array)
  1344. {
  1345. $origName = $this->getElementsBelongTo();
  1346. $name = $this->filterName($array, true);
  1347. if ('' === $name) {
  1348. $name = null;
  1349. }
  1350. $this->_elementsBelongTo = $name;
  1351. if (null === $name) {
  1352. $this->setIsArray(false);
  1353. if (null !== $origName) {
  1354. $this->_setElementsBelongTo();
  1355. }
  1356. } else {
  1357. $this->setIsArray(true);
  1358. $this->_setElementsBelongTo();
  1359. }
  1360. return $this;
  1361. }
  1362. /**
  1363. * Set array to which elements belong
  1364. *
  1365. * @param string $name Element name
  1366. * @return void
  1367. */
  1368. protected function _setElementsBelongTo($name = null)
  1369. {
  1370. $array = $this->getElementsBelongTo();
  1371. if (null === $array) {
  1372. return;
  1373. }
  1374. if (null === $name) {
  1375. foreach ($this->getElements() as $element) {
  1376. $element->setBelongsTo($array);
  1377. }
  1378. } else {
  1379. if (null !== ($element = $this->getElement($name))) {
  1380. $element->setBelongsTo($array);
  1381. }
  1382. }
  1383. }
  1384. /**
  1385. * Get name of array elements belong to
  1386. *
  1387. * @return string|null
  1388. */
  1389. public function getElementsBelongTo()
  1390. {
  1391. if ((null === $this->_elementsBelongTo) && $this->isArray()) {
  1392. $name = $this->getName();
  1393. if ('' !== (string)$name) {
  1394. return $name;
  1395. }
  1396. }
  1397. return $this->_elementsBelongTo;
  1398. }
  1399. /**
  1400. * Set flag indicating elements belong to array
  1401. *
  1402. * @param bool $flag Value of flag
  1403. * @return Zend_Form
  1404. */
  1405. public function setIsArray($flag)
  1406. {
  1407. $this->_isArray = (bool) $flag;
  1408. return $this;
  1409. }
  1410. /**
  1411. * Get flag indicating if elements belong to an array
  1412. *
  1413. * @return bool
  1414. */
  1415. public function isArray()
  1416. {
  1417. return $this->_isArray;
  1418. }
  1419. // Element groups:
  1420. /**
  1421. * Add a form group/subform
  1422. *
  1423. * @param Zend_Form $form
  1424. * @param string $name
  1425. * @param int $order
  1426. * @return Zend_Form
  1427. */
  1428. public function addSubForm(Zend_Form $form, $name, $order = null)
  1429. {
  1430. $name = (string) $name;
  1431. foreach ($this->_loaders as $type => $loader) {
  1432. $loaderPaths = $loader->getPaths();
  1433. foreach ($loaderPaths as $prefix => $paths) {
  1434. foreach ($paths as $path) {
  1435. $form->addPrefixPath($prefix, $path, $type);
  1436. }
  1437. }
  1438. }
  1439. if (!empty($this->_elementPrefixPaths)) {
  1440. foreach ($this->_elementPrefixPaths as $spec) {
  1441. list($prefix, $path, $type) = array_values($spec);
  1442. $form->addElementPrefixPath($prefix, $path, $type);
  1443. }
  1444. }
  1445. if (!empty($this->_displayGroupPrefixPaths)) {
  1446. foreach ($this->_displayGroupPrefixPaths as $spec) {
  1447. list($prefix, $path) = array_values($spec);
  1448. $form->addDisplayGroupPrefixPath($prefix, $path);
  1449. }
  1450. }
  1451. if (null !== $order) {
  1452. $form->setOrder($order);
  1453. }
  1454. if (($oldName = $form->getName()) &&
  1455. $oldName !== $name &&
  1456. $oldName === $form->getElementsBelongTo()) {
  1457. $form->setElementsBelongTo($name);
  1458. }
  1459. $form->setName($name);
  1460. $this->_subForms[$name] = $form;
  1461. $this->_order[$name] = $order;
  1462. $this->_orderUpdated = true;
  1463. return $this;
  1464. }
  1465. /**
  1466. * Add multiple form subForms/subforms at once
  1467. *
  1468. * @param array $subForms
  1469. * @return Zend_Form
  1470. */
  1471. public function addSubForms(array $subForms)
  1472. {
  1473. foreach ($subForms as $key => $spec) {
  1474. $name = null;
  1475. if (!is_numeric($key)) {
  1476. $name = $key;
  1477. }
  1478. if ($spec instanceof Zend_Form) {
  1479. $this->addSubForm($spec, $name);
  1480. continue;
  1481. }
  1482. if (is_array($spec)) {
  1483. $argc = count($spec);
  1484. $order = null;
  1485. switch ($argc) {
  1486. case 0:
  1487. continue;
  1488. case (1 <= $argc):
  1489. $subForm = array_shift($spec);
  1490. case (2 <= $argc):
  1491. $name = array_shift($spec);
  1492. case (3 <= $argc):
  1493. $order = array_shift($spec);
  1494. default:
  1495. $this->addSubForm($subForm, $name, $order);
  1496. }
  1497. }
  1498. }
  1499. return $this;
  1500. }
  1501. /**
  1502. * Set multiple form subForms/subforms (overwrites)
  1503. *
  1504. * @param array $subForms
  1505. * @return Zend_Form
  1506. */
  1507. public function setSubForms(array $subForms)
  1508. {
  1509. $this->clearSubForms();
  1510. return $this->addSubForms($subForms);
  1511. }
  1512. /**
  1513. * Retrieve a form subForm/subform
  1514. *
  1515. * @param string $name
  1516. * @return Zend_Form|null
  1517. */
  1518. public function getSubForm($name)
  1519. {
  1520. $name = (string) $name;
  1521. if (isset($this->_subForms[$name])) {
  1522. return $this->_subForms[$name];
  1523. }
  1524. return null;
  1525. }
  1526. /**
  1527. * Retrieve all form subForms/subforms
  1528. *
  1529. * @return array
  1530. */
  1531. public function getSubForms()
  1532. {
  1533. return $this->_subForms;
  1534. }
  1535. /**
  1536. * Remove form subForm/subform
  1537. *
  1538. * @param string $name
  1539. * @return boolean
  1540. */
  1541. public function removeSubForm($name)
  1542. {
  1543. $name = (string) $name;
  1544. if (array_key_exists($name, $this->_subForms)) {
  1545. unset($this->_subForms[$name]);
  1546. if (array_key_exists($name, $this->_order)) {
  1547. unset($this->_order[$name]);
  1548. $this->_orderUpdated = true;
  1549. }
  1550. return true;
  1551. }
  1552. return false;
  1553. }
  1554. /**
  1555. * Remove all form subForms/subforms
  1556. *
  1557. * @return Zend_Form
  1558. */
  1559. public function clearSubForms()
  1560. {
  1561. foreach (array_keys($this->_subForms) as $key) {
  1562. if (array_key_exists($key, $this->_order)) {
  1563. unset($this->_order[$key]);
  1564. }
  1565. }
  1566. $this->_subForms = array();
  1567. $this->_orderUpdated = true;
  1568. return $this;
  1569. }
  1570. // Display groups:
  1571. /**
  1572. * Set default display group class
  1573. *
  1574. * @param string $class
  1575. * @return Zend_Form
  1576. */
  1577. public function setDefaultDisplayGroupClass($class)
  1578. {
  1579. $this->_defaultDisplayGroupClass = (string) $class;
  1580. return $this;
  1581. }
  1582. /**
  1583. * Retrieve default display group class
  1584. *
  1585. * @return string
  1586. */
  1587. public function getDefaultDisplayGroupClass()
  1588. {
  1589. return $this->_defaultDisplayGroupClass;
  1590. }
  1591. /**
  1592. * Add a display group
  1593. *
  1594. * Groups named elements for display purposes.
  1595. *
  1596. * If a referenced element does not yet exist in the form, it is omitted.
  1597. *
  1598. * @param array $elements
  1599. * @param string $name
  1600. * @param array|Zend_Config $options
  1601. * @return Zend_Form
  1602. * @throws Zend_Form_Exception if no valid elements provided
  1603. */
  1604. public function addDisplayGroup(array $elements, $name, $options = null)
  1605. {
  1606. $group = array();
  1607. foreach ($elements as $element) {
  1608. if($element instanceof Zend_Form_Element) {
  1609. $elementName = $element->getName();
  1610. if (!isset($this->_elements[$elementName])) {
  1611. $this->addElement($element);
  1612. }
  1613. $element = $elementName;
  1614. }
  1615. if (isset($this->_elements[$element])) {
  1616. $add = $this->getElement($element);
  1617. if (null !== $add) {
  1618. $group[] = $add;
  1619. }
  1620. }
  1621. }
  1622. if (empty($group)) {
  1623. require_once 'Zend/Form/Exception.php';
  1624. throw new Zend_Form_Exception('No valid elements specified for display group');
  1625. }
  1626. $name = (string) $name;
  1627. if (is_array($options)) {
  1628. $options['form'] = $this;
  1629. $options['elements'] = $group;
  1630. } elseif ($options instanceof Zend_Config) {
  1631. $options = $options->toArray();
  1632. $options['form'] = $this;
  1633. $options['elements'] = $group;
  1634. } else {
  1635. $options = array(
  1636. 'form' => $this,
  1637. 'elements' => $group,
  1638. );
  1639. }
  1640. if (isset($options['displayGroupClass'])) {
  1641. $class = $options['displayGroupClass'];
  1642. unset($options['displayGroupClass']);
  1643. } else {
  1644. $class = $this->getDefaultDisplayGroupClass();
  1645. }
  1646. if (!class_exists($class)) {
  1647. require_once 'Zend/Loader.php';
  1648. Zend_Loader::loadClass($class);
  1649. }
  1650. $this->_displayGroups[$name] = new $class(
  1651. $name,
  1652. $this->getPluginLoader(self::DECORATOR),
  1653. $options
  1654. );
  1655. if (!em

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