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

/library/Zend/Form.php

https://bitbucket.org/mayorbrain/precurio-v2
PHP | 3108 lines | 1763 code | 321 blank | 1024 comment | 272 complexity | 91ec240718fc40e291281e7aea7cbaf0 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, MIT
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Form
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** @see Zend_Validate_Interface */
  21. require_once 'Zend/Validate/Interface.php';
  22. /**
  23. * Zend_Form
  24. *
  25. * @category Zend
  26. * @package Zend_Form
  27. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. * @version $Id: Form.php 20096 2010-01-06 02:05:09Z bkarwin $
  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. * Constructor
  188. *
  189. * Registers form view helper as decorator
  190. *
  191. * @param mixed $options
  192. * @return void
  193. */
  194. public function __construct($options = null)
  195. {
  196. if (is_array($options)) {
  197. $this->setOptions($options);
  198. } elseif ($options instanceof Zend_Config) {
  199. $this->setConfig($options);
  200. }
  201. // Extensions...
  202. $this->init();
  203. $this->loadDefaultDecorators();
  204. }
  205. /**
  206. * Clone form object and all children
  207. *
  208. * @return void
  209. */
  210. public function __clone()
  211. {
  212. $elements = array();
  213. foreach ($this->getElements() as $name => $element) {
  214. $elements[] = clone $element;
  215. }
  216. $this->setElements($elements);
  217. $subForms = array();
  218. foreach ($this->getSubForms() as $name => $subForm) {
  219. $subForms[$name] = clone $subForm;
  220. }
  221. $this->setSubForms($subForms);
  222. $displayGroups = array();
  223. foreach ($this->_displayGroups as $group) {
  224. $clone = clone $group;
  225. $elements = array();
  226. foreach ($clone->getElements() as $name => $e) {
  227. $elements[] = $this->getElement($name);
  228. }
  229. $clone->setElements($elements);
  230. $displayGroups[] = $clone;
  231. }
  232. $this->setDisplayGroups($displayGroups);
  233. }
  234. /**
  235. * Reset values of form
  236. *
  237. * @return Zend_Form
  238. */
  239. public function reset()
  240. {
  241. foreach ($this->getElements() as $element) {
  242. $element->setValue(null);
  243. }
  244. foreach ($this->getSubForms() as $subForm) {
  245. $subForm->reset();
  246. }
  247. return $this;
  248. }
  249. /**
  250. * Initialize form (used by extending classes)
  251. *
  252. * @return void
  253. */
  254. public function init()
  255. {
  256. }
  257. /**
  258. * Set form state from options array
  259. *
  260. * @param array $options
  261. * @return Zend_Form
  262. */
  263. public function setOptions(array $options)
  264. {
  265. if (isset($options['prefixPath'])) {
  266. $this->addPrefixPaths($options['prefixPath']);
  267. unset($options['prefixPath']);
  268. }
  269. if (isset($options['elementPrefixPath'])) {
  270. $this->addElementPrefixPaths($options['elementPrefixPath']);
  271. unset($options['elementPrefixPath']);
  272. }
  273. if (isset($options['displayGroupPrefixPath'])) {
  274. $this->addDisplayGroupPrefixPaths($options['displayGroupPrefixPath']);
  275. unset($options['displayGroupPrefixPath']);
  276. }
  277. if (isset($options['elementDecorators'])) {
  278. $this->_elementDecorators = $options['elementDecorators'];
  279. unset($options['elementDecorators']);
  280. }
  281. if (isset($options['elements'])) {
  282. $this->setElements($options['elements']);
  283. unset($options['elements']);
  284. }
  285. if (isset($options['defaultDisplayGroupClass'])) {
  286. $this->setDefaultDisplayGroupClass($options['defaultDisplayGroupClass']);
  287. unset($options['defaultDisplayGroupClass']);
  288. }
  289. if (isset($options['displayGroupDecorators'])) {
  290. $displayGroupDecorators = $options['displayGroupDecorators'];
  291. unset($options['displayGroupDecorators']);
  292. }
  293. if (isset($options['elementsBelongTo'])) {
  294. $elementsBelongTo = $options['elementsBelongTo'];
  295. unset($options['elementsBelongTo']);
  296. }
  297. if (isset($options['attribs'])) {
  298. $this->addAttribs($options['attribs']);
  299. unset($options['attribs']);
  300. }
  301. $forbidden = array(
  302. 'Options', 'Config', 'PluginLoader', 'SubForms', 'View', 'Translator',
  303. 'Attrib', 'Default',
  304. );
  305. foreach ($options as $key => $value) {
  306. $normalized = ucfirst($key);
  307. if (in_array($normalized, $forbidden)) {
  308. continue;
  309. }
  310. $method = 'set' . $normalized;
  311. if (method_exists($this, $method)) {
  312. $this->$method($value);
  313. } else {
  314. $this->setAttrib($key, $value);
  315. }
  316. }
  317. if (isset($displayGroupDecorators)) {
  318. $this->setDisplayGroupDecorators($displayGroupDecorators);
  319. }
  320. if (isset($elementsBelongTo)) {
  321. $this->setElementsBelongTo($elementsBelongTo);
  322. }
  323. return $this;
  324. }
  325. /**
  326. * Set form state from config object
  327. *
  328. * @param Zend_Config $config
  329. * @return Zend_Form
  330. */
  331. public function setConfig(Zend_Config $config)
  332. {
  333. return $this->setOptions($config->toArray());
  334. }
  335. // Loaders
  336. /**
  337. * Set plugin loaders for use with decorators and elements
  338. *
  339. * @param Zend_Loader_PluginLoader_Interface $loader
  340. * @param string $type 'decorator' or 'element'
  341. * @return Zend_Form
  342. * @throws Zend_Form_Exception on invalid type
  343. */
  344. public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type = null)
  345. {
  346. $type = strtoupper($type);
  347. switch ($type) {
  348. case self::DECORATOR:
  349. case self::ELEMENT:
  350. $this->_loaders[$type] = $loader;
  351. return $this;
  352. default:
  353. require_once 'Zend/Form/Exception.php';
  354. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type));
  355. }
  356. }
  357. /**
  358. * Retrieve plugin loader for given type
  359. *
  360. * $type may be one of:
  361. * - decorator
  362. * - element
  363. *
  364. * If a plugin loader does not exist for the given type, defaults are
  365. * created.
  366. *
  367. * @param string $type
  368. * @return Zend_Loader_PluginLoader_Interface
  369. */
  370. public function getPluginLoader($type = null)
  371. {
  372. $type = strtoupper($type);
  373. if (!isset($this->_loaders[$type])) {
  374. switch ($type) {
  375. case self::DECORATOR:
  376. $prefixSegment = 'Form_Decorator';
  377. $pathSegment = 'Form/Decorator';
  378. break;
  379. case self::ELEMENT:
  380. $prefixSegment = 'Form_Element';
  381. $pathSegment = 'Form/Element';
  382. break;
  383. default:
  384. require_once 'Zend/Form/Exception.php';
  385. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  386. }
  387. require_once 'Zend/Loader/PluginLoader.php';
  388. $this->_loaders[$type] = new Zend_Loader_PluginLoader(
  389. array('Zend_' . $prefixSegment . '_' => 'Zend/' . $pathSegment . '/')
  390. );
  391. }
  392. return $this->_loaders[$type];
  393. }
  394. /**
  395. * Add prefix path for plugin loader
  396. *
  397. * If no $type specified, assumes it is a base path for both filters and
  398. * validators, and sets each according to the following rules:
  399. * - decorators: $prefix = $prefix . '_Decorator'
  400. * - elements: $prefix = $prefix . '_Element'
  401. *
  402. * Otherwise, the path prefix is set on the appropriate plugin loader.
  403. *
  404. * If $type is 'decorators', sets the path in the decorator plugin loader
  405. * for all elements. Additionally, if no $type is provided,
  406. * {@link Zend_Form_Element::addPrefixPath()} is called on each element.
  407. *
  408. * @param string $prefix
  409. * @param string $path
  410. * @param string $type
  411. * @return Zend_Form
  412. * @throws Zend_Form_Exception for invalid type
  413. */
  414. public function addPrefixPath($prefix, $path, $type = null)
  415. {
  416. $type = strtoupper($type);
  417. switch ($type) {
  418. case self::DECORATOR:
  419. case self::ELEMENT:
  420. $loader = $this->getPluginLoader($type);
  421. $loader->addPrefixPath($prefix, $path);
  422. return $this;
  423. case null:
  424. $prefix = rtrim($prefix, '_');
  425. $path = rtrim($path, DIRECTORY_SEPARATOR);
  426. foreach (array(self::DECORATOR, self::ELEMENT) as $type) {
  427. $cType = ucfirst(strtolower($type));
  428. $pluginPath = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR;
  429. $pluginPrefix = $prefix . '_' . $cType;
  430. $loader = $this->getPluginLoader($type);
  431. $loader->addPrefixPath($pluginPrefix, $pluginPath);
  432. }
  433. return $this;
  434. default:
  435. require_once 'Zend/Form/Exception.php';
  436. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  437. }
  438. }
  439. /**
  440. * Add many prefix paths at once
  441. *
  442. * @param array $spec
  443. * @return Zend_Form
  444. */
  445. public function addPrefixPaths(array $spec)
  446. {
  447. if (isset($spec['prefix']) && isset($spec['path'])) {
  448. return $this->addPrefixPath($spec['prefix'], $spec['path']);
  449. }
  450. foreach ($spec as $type => $paths) {
  451. if (is_numeric($type) && is_array($paths)) {
  452. $type = null;
  453. if (isset($paths['prefix']) && isset($paths['path'])) {
  454. if (isset($paths['type'])) {
  455. $type = $paths['type'];
  456. }
  457. $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
  458. }
  459. } elseif (!is_numeric($type)) {
  460. if (!isset($paths['prefix']) || !isset($paths['path'])) {
  461. continue;
  462. }
  463. $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
  464. }
  465. }
  466. return $this;
  467. }
  468. /**
  469. * Add prefix path for all elements
  470. *
  471. * @param string $prefix
  472. * @param string $path
  473. * @param string $type
  474. * @return Zend_Form
  475. */
  476. public function addElementPrefixPath($prefix, $path, $type = null)
  477. {
  478. $this->_elementPrefixPaths[] = array(
  479. 'prefix' => $prefix,
  480. 'path' => $path,
  481. 'type' => $type,
  482. );
  483. foreach ($this->getElements() as $element) {
  484. $element->addPrefixPath($prefix, $path, $type);
  485. }
  486. foreach ($this->getSubForms() as $subForm) {
  487. $subForm->addElementPrefixPath($prefix, $path, $type);
  488. }
  489. return $this;
  490. }
  491. /**
  492. * Add prefix paths for all elements
  493. *
  494. * @param array $spec
  495. * @return Zend_Form
  496. */
  497. public function addElementPrefixPaths(array $spec)
  498. {
  499. $this->_elementPrefixPaths = $this->_elementPrefixPaths + $spec;
  500. foreach ($this->getElements() as $element) {
  501. $element->addPrefixPaths($spec);
  502. }
  503. return $this;
  504. }
  505. /**
  506. * Add prefix path for all display groups
  507. *
  508. * @param string $prefix
  509. * @param string $path
  510. * @return Zend_Form
  511. */
  512. public function addDisplayGroupPrefixPath($prefix, $path)
  513. {
  514. $this->_displayGroupPrefixPaths[] = array(
  515. 'prefix' => $prefix,
  516. 'path' => $path,
  517. );
  518. foreach ($this->getDisplayGroups() as $group) {
  519. $group->addPrefixPath($prefix, $path);
  520. }
  521. return $this;
  522. }
  523. /**
  524. * Add multiple display group prefix paths at once
  525. *
  526. * @param array $spec
  527. * @return Zend_Form
  528. */
  529. public function addDisplayGroupPrefixPaths(array $spec)
  530. {
  531. foreach ($spec as $key => $value) {
  532. if (is_string($value) && !is_numeric($key)) {
  533. $this->addDisplayGroupPrefixPath($key, $value);
  534. continue;
  535. }
  536. if (is_string($value) && is_numeric($key)) {
  537. continue;
  538. }
  539. if (is_array($value)) {
  540. $count = count($value);
  541. if (array_keys($value) === range(0, $count - 1)) {
  542. if ($count < 2) {
  543. continue;
  544. }
  545. $prefix = array_shift($value);
  546. $path = array_shift($value);
  547. $this->addDisplayGroupPrefixPath($prefix, $path);
  548. continue;
  549. }
  550. if (array_key_exists('prefix', $value) && array_key_exists('path', $value)) {
  551. $this->addDisplayGroupPrefixPath($value['prefix'], $value['path']);
  552. }
  553. }
  554. }
  555. return $this;
  556. }
  557. // Form metadata:
  558. /**
  559. * Set form attribute
  560. *
  561. * @param string $key
  562. * @param mixed $value
  563. * @return Zend_Form
  564. */
  565. public function setAttrib($key, $value)
  566. {
  567. $key = (string) $key;
  568. $this->_attribs[$key] = $value;
  569. return $this;
  570. }
  571. /**
  572. * Add multiple form attributes at once
  573. *
  574. * @param array $attribs
  575. * @return Zend_Form
  576. */
  577. public function addAttribs(array $attribs)
  578. {
  579. foreach ($attribs as $key => $value) {
  580. $this->setAttrib($key, $value);
  581. }
  582. return $this;
  583. }
  584. /**
  585. * Set multiple form attributes at once
  586. *
  587. * Overwrites any previously set attributes.
  588. *
  589. * @param array $attribs
  590. * @return Zend_Form
  591. */
  592. public function setAttribs(array $attribs)
  593. {
  594. $this->clearAttribs();
  595. return $this->addAttribs($attribs);
  596. }
  597. /**
  598. * Retrieve a single form attribute
  599. *
  600. * @param string $key
  601. * @return mixed
  602. */
  603. public function getAttrib($key)
  604. {
  605. $key = (string) $key;
  606. if (!isset($this->_attribs[$key])) {
  607. return null;
  608. }
  609. return $this->_attribs[$key];
  610. }
  611. /**
  612. * Retrieve all form attributes/metadata
  613. *
  614. * @return array
  615. */
  616. public function getAttribs()
  617. {
  618. return $this->_attribs;
  619. }
  620. /**
  621. * Remove attribute
  622. *
  623. * @param string $key
  624. * @return bool
  625. */
  626. public function removeAttrib($key)
  627. {
  628. if (isset($this->_attribs[$key])) {
  629. unset($this->_attribs[$key]);
  630. return true;
  631. }
  632. return false;
  633. }
  634. /**
  635. * Clear all form attributes
  636. *
  637. * @return Zend_Form
  638. */
  639. public function clearAttribs()
  640. {
  641. $this->_attribs = array();
  642. return $this;
  643. }
  644. /**
  645. * Set form action
  646. *
  647. * @param string $action
  648. * @return Zend_Form
  649. */
  650. public function setAction($action)
  651. {
  652. return $this->setAttrib('action', (string) $action);
  653. }
  654. /**
  655. * Get form action
  656. *
  657. * Sets default to '' if not set.
  658. *
  659. * @return string
  660. */
  661. public function getAction()
  662. {
  663. $action = $this->getAttrib('action');
  664. if (null === $action) {
  665. $action = '';
  666. $this->setAction($action);
  667. }
  668. return $action;
  669. }
  670. /**
  671. * Set form method
  672. *
  673. * Only values in {@link $_methods()} allowed
  674. *
  675. * @param string $method
  676. * @return Zend_Form
  677. * @throws Zend_Form_Exception
  678. */
  679. public function setMethod($method)
  680. {
  681. $method = strtolower($method);
  682. if (!in_array($method, $this->_methods)) {
  683. require_once 'Zend/Form/Exception.php';
  684. throw new Zend_Form_Exception(sprintf('"%s" is an invalid form method', $method));
  685. }
  686. $this->setAttrib('method', $method);
  687. return $this;
  688. }
  689. /**
  690. * Retrieve form method
  691. *
  692. * @return string
  693. */
  694. public function getMethod()
  695. {
  696. if (null === ($method = $this->getAttrib('method'))) {
  697. $method = self::METHOD_POST;
  698. $this->setAttrib('method', $method);
  699. }
  700. return strtolower($method);
  701. }
  702. /**
  703. * Set encoding type
  704. *
  705. * @param string $value
  706. * @return Zend_Form
  707. */
  708. public function setEnctype($value)
  709. {
  710. $this->setAttrib('enctype', $value);
  711. return $this;
  712. }
  713. /**
  714. * Get encoding type
  715. *
  716. * @return string
  717. */
  718. public function getEnctype()
  719. {
  720. if (null === ($enctype = $this->getAttrib('enctype'))) {
  721. $enctype = self::ENCTYPE_URLENCODED;
  722. $this->setAttrib('enctype', $enctype);
  723. }
  724. return $this->getAttrib('enctype');
  725. }
  726. /**
  727. * Filter a name to only allow valid variable characters
  728. *
  729. * @param string $value
  730. * @param bool $allowBrackets
  731. * @return string
  732. */
  733. public function filterName($value, $allowBrackets = false)
  734. {
  735. $charset = '^a-zA-Z0-9_\x7f-\xff';
  736. if ($allowBrackets) {
  737. $charset .= '\[\]';
  738. }
  739. return preg_replace('/[' . $charset . ']/', '', (string) $value);
  740. }
  741. /**
  742. * Set form name
  743. *
  744. * @param string $name
  745. * @return Zend_Form
  746. */
  747. public function setName($name)
  748. {
  749. $name = $this->filterName($name);
  750. if (('0' !== $name) && empty($name)) {
  751. require_once 'Zend/Form/Exception.php';
  752. throw new Zend_Form_Exception('Invalid name provided; must contain only valid variable characters and be non-empty');
  753. }
  754. return $this->setAttrib('name', $name);
  755. }
  756. /**
  757. * Get name attribute
  758. *
  759. * @return null|string
  760. */
  761. public function getName()
  762. {
  763. return $this->getAttrib('name');
  764. }
  765. /**
  766. * Get fully qualified name
  767. *
  768. * Places name as subitem of array and/or appends brackets.
  769. *
  770. * @return string
  771. */
  772. public function getFullyQualifiedName()
  773. {
  774. return $this->getName();
  775. }
  776. /**
  777. * Get element id
  778. *
  779. * @return string
  780. */
  781. public function getId()
  782. {
  783. if (null !== ($id = $this->getAttrib('id'))) {
  784. return $id;
  785. }
  786. $id = $this->getFullyQualifiedName();
  787. // Bail early if no array notation detected
  788. if (!strstr($id, '[')) {
  789. return $id;
  790. }
  791. // Strip array notation
  792. if ('[]' == substr($id, -2)) {
  793. $id = substr($id, 0, strlen($id) - 2);
  794. }
  795. $id = str_replace('][', '-', $id);
  796. $id = str_replace(array(']', '['), '-', $id);
  797. $id = trim($id, '-');
  798. return $id;
  799. }
  800. /**
  801. * Set form legend
  802. *
  803. * @param string $value
  804. * @return Zend_Form
  805. */
  806. public function setLegend($value)
  807. {
  808. $this->_legend = (string) $value;
  809. return $this;
  810. }
  811. /**
  812. * Get form legend
  813. *
  814. * @return string
  815. */
  816. public function getLegend()
  817. {
  818. return $this->_legend;
  819. }
  820. /**
  821. * Set form description
  822. *
  823. * @param string $value
  824. * @return Zend_Form
  825. */
  826. public function setDescription($value)
  827. {
  828. $this->_description = (string) $value;
  829. return $this;
  830. }
  831. /**
  832. * Retrieve form description
  833. *
  834. * @return string
  835. */
  836. public function getDescription()
  837. {
  838. return $this->_description;
  839. }
  840. /**
  841. * Set form order
  842. *
  843. * @param int $index
  844. * @return Zend_Form
  845. */
  846. public function setOrder($index)
  847. {
  848. $this->_formOrder = (int) $index;
  849. return $this;
  850. }
  851. /**
  852. * Get form order
  853. *
  854. * @return int|null
  855. */
  856. public function getOrder()
  857. {
  858. return $this->_formOrder;
  859. }
  860. // Element interaction:
  861. /**
  862. * Add a new element
  863. *
  864. * $element may be either a string element type, or an object of type
  865. * Zend_Form_Element. If a string element type is provided, $name must be
  866. * provided, and $options may be optionally provided for configuring the
  867. * element.
  868. *
  869. * If a Zend_Form_Element is provided, $name may be optionally provided,
  870. * and any provided $options will be ignored.
  871. *
  872. * @param string|Zend_Form_Element $element
  873. * @param string $name
  874. * @param array|Zend_Config $options
  875. * @return Zend_Form
  876. */
  877. public function addElement($element, $name = null, $options = null)
  878. {
  879. if (is_string($element)) {
  880. if (null === $name) {
  881. require_once 'Zend/Form/Exception.php';
  882. throw new Zend_Form_Exception('Elements specified by string must have an accompanying name');
  883. }
  884. if (is_array($this->_elementDecorators)) {
  885. if (null === $options) {
  886. $options = array('decorators' => $this->_elementDecorators);
  887. } elseif ($options instanceof Zend_Config) {
  888. $options = $options->toArray();
  889. }
  890. if (is_array($options)
  891. && !array_key_exists('decorators', $options)
  892. ) {
  893. $options['decorators'] = $this->_elementDecorators;
  894. }
  895. }
  896. $this->_elements[$name] = $this->createElement($element, $name, $options);
  897. } elseif ($element instanceof Zend_Form_Element) {
  898. $prefixPaths = array();
  899. $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
  900. if (!empty($this->_elementPrefixPaths)) {
  901. $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
  902. }
  903. if (null === $name) {
  904. $name = $element->getName();
  905. }
  906. $this->_elements[$name] = $element;
  907. $this->_elements[$name]->addPrefixPaths($prefixPaths);
  908. }
  909. $this->_order[$name] = $this->_elements[$name]->getOrder();
  910. $this->_orderUpdated = true;
  911. $this->_setElementsBelongTo($name);
  912. return $this;
  913. }
  914. /**
  915. * Create an element
  916. *
  917. * Acts as a factory for creating elements. Elements created with this
  918. * method will not be attached to the form, but will contain element
  919. * settings as specified in the form object (including plugin loader
  920. * prefix paths, default decorators, etc.).
  921. *
  922. * @param string $type
  923. * @param string $name
  924. * @param array|Zend_Config $options
  925. * @return Zend_Form_Element
  926. */
  927. public function createElement($type, $name, $options = null)
  928. {
  929. if (!is_string($type)) {
  930. require_once 'Zend/Form/Exception.php';
  931. throw new Zend_Form_Exception('Element type must be a string indicating type');
  932. }
  933. if (!is_string($name)) {
  934. require_once 'Zend/Form/Exception.php';
  935. throw new Zend_Form_Exception('Element name must be a string');
  936. }
  937. $prefixPaths = array();
  938. $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
  939. if (!empty($this->_elementPrefixPaths)) {
  940. $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
  941. }
  942. if ($options instanceof Zend_Config) {
  943. $options = $options->toArray();
  944. }
  945. if ((null === $options) || !is_array($options)) {
  946. $options = array('prefixPath' => $prefixPaths);
  947. } elseif (is_array($options)) {
  948. if (array_key_exists('prefixPath', $options)) {
  949. $options['prefixPath'] = array_merge($prefixPaths, $options['prefixPath']);
  950. } else {
  951. $options['prefixPath'] = $prefixPaths;
  952. }
  953. }
  954. $class = $this->getPluginLoader(self::ELEMENT)->load($type);
  955. $element = new $class($name, $options);
  956. return $element;
  957. }
  958. /**
  959. * Add multiple elements at once
  960. *
  961. * @param array $elements
  962. * @return Zend_Form
  963. */
  964. public function addElements(array $elements)
  965. {
  966. foreach ($elements as $key => $spec) {
  967. $name = null;
  968. if (!is_numeric($key)) {
  969. $name = $key;
  970. }
  971. if (is_string($spec) || ($spec instanceof Zend_Form_Element)) {
  972. $this->addElement($spec, $name);
  973. continue;
  974. }
  975. if (is_array($spec)) {
  976. $argc = count($spec);
  977. $options = array();
  978. if (isset($spec['type'])) {
  979. $type = $spec['type'];
  980. if (isset($spec['name'])) {
  981. $name = $spec['name'];
  982. }
  983. if (isset($spec['options'])) {
  984. $options = $spec['options'];
  985. }
  986. $this->addElement($type, $name, $options);
  987. } else {
  988. switch ($argc) {
  989. case 0:
  990. continue;
  991. case (1 <= $argc):
  992. $type = array_shift($spec);
  993. case (2 <= $argc):
  994. if (null === $name) {
  995. $name = array_shift($spec);
  996. } else {
  997. $options = array_shift($spec);
  998. }
  999. case (3 <= $argc):
  1000. if (empty($options)) {
  1001. $options = array_shift($spec);
  1002. }
  1003. default:
  1004. $this->addElement($type, $name, $options);
  1005. }
  1006. }
  1007. }
  1008. }
  1009. return $this;
  1010. }
  1011. /**
  1012. * Set form elements (overwrites existing elements)
  1013. *
  1014. * @param array $elements
  1015. * @return Zend_Form
  1016. */
  1017. public function setElements(array $elements)
  1018. {
  1019. $this->clearElements();
  1020. return $this->addElements($elements);
  1021. }
  1022. /**
  1023. * Retrieve a single element
  1024. *
  1025. * @param string $name
  1026. * @return Zend_Form_Element|null
  1027. */
  1028. public function getElement($name)
  1029. {
  1030. if (array_key_exists($name, $this->_elements)) {
  1031. return $this->_elements[$name];
  1032. }
  1033. return null;
  1034. }
  1035. /**
  1036. * Retrieve all elements
  1037. *
  1038. * @return array
  1039. */
  1040. public function getElements()
  1041. {
  1042. return $this->_elements;
  1043. }
  1044. /**
  1045. * Remove element
  1046. *
  1047. * @param string $name
  1048. * @return boolean
  1049. */
  1050. public function removeElement($name)
  1051. {
  1052. $name = (string) $name;
  1053. if (isset($this->_elements[$name])) {
  1054. unset($this->_elements[$name]);
  1055. if (array_key_exists($name, $this->_order)) {
  1056. unset($this->_order[$name]);
  1057. $this->_orderUpdated = true;
  1058. } else {
  1059. foreach ($this->_displayGroups as $group) {
  1060. if (null !== $group->getElement($name)) {
  1061. $group->removeElement($name);
  1062. }
  1063. }
  1064. }
  1065. return true;
  1066. }
  1067. return false;
  1068. }
  1069. /**
  1070. * Remove all form elements
  1071. *
  1072. * @return Zend_Form
  1073. */
  1074. public function clearElements()
  1075. {
  1076. foreach (array_keys($this->_elements) as $key) {
  1077. if (array_key_exists($key, $this->_order)) {
  1078. unset($this->_order[$key]);
  1079. }
  1080. }
  1081. $this->_elements = array();
  1082. $this->_orderUpdated = true;
  1083. return $this;
  1084. }
  1085. /**
  1086. * Set default values for elements
  1087. *
  1088. * Sets values for all elements specified in the array of $defaults.
  1089. *
  1090. * @param array $defaults
  1091. * @return Zend_Form
  1092. */
  1093. public function setDefaults(array $defaults)
  1094. {
  1095. foreach ($this->getElements() as $name => $element) {
  1096. if (array_key_exists($name, $defaults)) {
  1097. $this->setDefault($name, $defaults[$name]);
  1098. }
  1099. }
  1100. foreach ($this->getSubForms() as $name => $form) {
  1101. if (array_key_exists($name, $defaults)) {
  1102. $form->setDefaults($defaults[$name]);
  1103. } else {
  1104. $form->setDefaults($defaults);
  1105. }
  1106. }
  1107. return $this;
  1108. }
  1109. /**
  1110. * Set default value for an element
  1111. *
  1112. * @param string $name
  1113. * @param mixed $value
  1114. * @return Zend_Form
  1115. */
  1116. public function setDefault($name, $value)
  1117. {
  1118. $name = (string) $name;
  1119. if ($element = $this->getElement($name)) {
  1120. $element->setValue($value);
  1121. } else {
  1122. if (is_scalar($value)) {
  1123. foreach ($this->getSubForms() as $subForm) {
  1124. $subForm->setDefault($name, $value);
  1125. }
  1126. } elseif (is_array($value) && ($subForm = $this->getSubForm($name))) {
  1127. $subForm->setDefaults($value);
  1128. }
  1129. }
  1130. return $this;
  1131. }
  1132. /**
  1133. * Retrieve value for single element
  1134. *
  1135. * @param string $name
  1136. * @return mixed
  1137. */
  1138. public function getValue($name)
  1139. {
  1140. if ($element = $this->getElement($name)) {
  1141. return $element->getValue();
  1142. }
  1143. if ($subForm = $this->getSubForm($name)) {
  1144. return $subForm->getValues(true);
  1145. }
  1146. foreach ($this->getSubForms() as $subForm) {
  1147. if ($name == $subForm->getElementsBelongTo()) {
  1148. return $subForm->getValues(true);
  1149. }
  1150. }
  1151. return null;
  1152. }
  1153. /**
  1154. * Retrieve all form element values
  1155. *
  1156. * @param bool $suppressArrayNotation
  1157. * @return array
  1158. */
  1159. public function getValues($suppressArrayNotation = false)
  1160. {
  1161. $values = array();
  1162. foreach ($this->getElements() as $key => $element) {
  1163. if (!$element->getIgnore()) {
  1164. $values[$key] = $element->getValue();
  1165. }
  1166. }
  1167. foreach ($this->getSubForms() as $key => $subForm) {
  1168. $fValues = $this->_attachToArray($subForm->getValues(true), $subForm->getElementsBelongTo());
  1169. $values = array_merge($values, $fValues);
  1170. }
  1171. if (!$suppressArrayNotation && $this->isArray()) {
  1172. $values = $this->_attachToArray($values, $this->getElementsBelongTo());
  1173. }
  1174. return $values;
  1175. }
  1176. /**
  1177. * Returns only the valid values from the given form input.
  1178. *
  1179. * For models that can be saved in a partially valid state, for example when following the builder,
  1180. * prototype or state patterns it is particularly interessting to retrieve all the current valid
  1181. * values to persist them.
  1182. *
  1183. * @param array $data
  1184. * @return array
  1185. */
  1186. public function getValidValues($data)
  1187. {
  1188. $values = array();
  1189. foreach ($this->getElements() as $key => $element) {
  1190. if (isset($data[$key])) {
  1191. if($element->isValid($data[$key], $data)) {
  1192. $values[$key] = $element->getValue();
  1193. }
  1194. }
  1195. }
  1196. foreach ($this->getSubForms() as $key => $form) {
  1197. if (isset($data[$key])) {
  1198. $values[$key] = $form->getValidValues($data[$key]);
  1199. }
  1200. }
  1201. return $values;
  1202. }
  1203. /**
  1204. * Get unfiltered element value
  1205. *
  1206. * @param string $name
  1207. * @return mixed
  1208. */
  1209. public function getUnfilteredValue($name)
  1210. {
  1211. if ($element = $this->getElement($name)) {
  1212. return $element->getUnfilteredValue();
  1213. }
  1214. return null;
  1215. }
  1216. /**
  1217. * Retrieve all unfiltered element values
  1218. *
  1219. * @return array
  1220. */
  1221. public function getUnfilteredValues()
  1222. {
  1223. $values = array();
  1224. foreach ($this->getElements() as $key => $element) {
  1225. $values[$key] = $element->getUnfilteredValue();
  1226. }
  1227. return $values;
  1228. }
  1229. /**
  1230. * Set all elements' filters
  1231. *
  1232. * @param array $filters
  1233. * @return Zend_Form
  1234. */
  1235. public function setElementFilters(array $filters)
  1236. {
  1237. foreach ($this->getElements() as $element) {
  1238. $element->setFilters($filters);
  1239. }
  1240. return $this;
  1241. }
  1242. /**
  1243. * Set name of array elements belong to
  1244. *
  1245. * @param string $array
  1246. * @return Zend_Form
  1247. */
  1248. public function setElementsBelongTo($array)
  1249. {
  1250. $origName = $this->getElementsBelongTo();
  1251. $name = $this->filterName($array, true);
  1252. if (empty($name)) {
  1253. $name = null;
  1254. }
  1255. $this->_elementsBelongTo = $name;
  1256. if (null === $name) {
  1257. $this->setIsArray(false);
  1258. if (null !== $origName) {
  1259. $this->_setElementsBelongTo();
  1260. }
  1261. } else {
  1262. $this->setIsArray(true);
  1263. $this->_setElementsBelongTo();
  1264. }
  1265. return $this;
  1266. }
  1267. /**
  1268. * Set array to which elements belong
  1269. *
  1270. * @param string $name Element name
  1271. * @return void
  1272. */
  1273. protected function _setElementsBelongTo($name = null)
  1274. {
  1275. $array = $this->getElementsBelongTo();
  1276. if (null === $array) {
  1277. return;
  1278. }
  1279. if (null === $name) {
  1280. foreach ($this->getElements() as $element) {
  1281. $element->setBelongsTo($array);
  1282. }
  1283. } else {
  1284. if (null !== ($element = $this->getElement($name))) {
  1285. $element->setBelongsTo($array);
  1286. }
  1287. }
  1288. }
  1289. /**
  1290. * Get name of array elements belong to
  1291. *
  1292. * @return string|null
  1293. */
  1294. public function getElementsBelongTo()
  1295. {
  1296. if ((null === $this->_elementsBelongTo) && $this->isArray()) {
  1297. $name = $this->getName();
  1298. if (!empty($name)) {
  1299. return $name;
  1300. }
  1301. }
  1302. return $this->_elementsBelongTo;
  1303. }
  1304. /**
  1305. * Set flag indicating elements belong to array
  1306. *
  1307. * @param bool $flag Value of flag
  1308. * @return Zend_Form
  1309. */
  1310. public function setIsArray($flag)
  1311. {
  1312. $this->_isArray = (bool) $flag;
  1313. return $this;
  1314. }
  1315. /**
  1316. * Get flag indicating if elements belong to an array
  1317. *
  1318. * @return bool
  1319. */
  1320. public function isArray()
  1321. {
  1322. return $this->_isArray;
  1323. }
  1324. // Element groups:
  1325. /**
  1326. * Add a form group/subform
  1327. *
  1328. * @param Zend_Form $form
  1329. * @param string $name
  1330. * @param int $order
  1331. * @return Zend_Form
  1332. */
  1333. public function addSubForm(Zend_Form $form, $name, $order = null)
  1334. {
  1335. $name = (string) $name;
  1336. foreach ($this->_loaders as $type => $loader) {
  1337. $loaderPaths = $loader->getPaths();
  1338. foreach ($loaderPaths as $prefix => $paths) {
  1339. foreach ($paths as $path) {
  1340. $form->addPrefixPath($prefix, $path, $type);
  1341. }
  1342. }
  1343. }
  1344. if (!empty($this->_elementPrefixPaths)) {
  1345. foreach ($this->_elementPrefixPaths as $spec) {
  1346. list($prefix, $path, $type) = array_values($spec);
  1347. $form->addElementPrefixPath($prefix, $path, $type);
  1348. }
  1349. }
  1350. if (!empty($this->_displayGroupPrefixPaths)) {
  1351. foreach ($this->_displayGroupPrefixPaths as $spec) {
  1352. list($prefix, $path) = array_values($spec);
  1353. $form->addDisplayGroupPrefixPath($prefix, $path);
  1354. }
  1355. }
  1356. if (null !== $order) {
  1357. $form->setOrder($order);
  1358. }
  1359. $form->setName($name);
  1360. $this->_subForms[$name] = $form;
  1361. $this->_order[$name] = $order;
  1362. $this->_orderUpdated = true;
  1363. return $this;
  1364. }
  1365. /**
  1366. * Add multiple form subForms/subforms at once
  1367. *
  1368. * @param array $subForms
  1369. * @return Zend_Form
  1370. */
  1371. public function addSubForms(array $subForms)
  1372. {
  1373. foreach ($subForms as $key => $spec) {
  1374. $name = null;
  1375. if (!is_numeric($key)) {
  1376. $name = $key;
  1377. }
  1378. if ($spec instanceof Zend_Form) {
  1379. $this->addSubForm($spec, $name);
  1380. continue;
  1381. }
  1382. if (is_array($spec)) {
  1383. $argc = count($spec);
  1384. $order = null;
  1385. switch ($argc) {
  1386. case 0:
  1387. continue;
  1388. case (1 <= $argc):
  1389. $subForm = array_shift($spec);
  1390. case (2 <= $argc):
  1391. $name = array_shift($spec);
  1392. case (3 <= $argc):
  1393. $order = array_shift($spec);
  1394. default:
  1395. $this->addSubForm($subForm, $name, $order);
  1396. }
  1397. }
  1398. }
  1399. return $this;
  1400. }
  1401. /**
  1402. * Set multiple form subForms/subforms (overwrites)
  1403. *
  1404. * @param array $subForms
  1405. * @return Zend_Form
  1406. */
  1407. public function setSubForms(array $subForms)
  1408. {
  1409. $this->clearSubForms();
  1410. return $this->addSubForms($subForms);
  1411. }
  1412. /**
  1413. * Retrieve a form subForm/subform
  1414. *
  1415. * @param string $name
  1416. * @return Zend_Form|null
  1417. */
  1418. public function getSubForm($name)
  1419. {
  1420. $name = (string) $name;
  1421. if (isset($this->_subForms[$name])) {
  1422. return $this->_subForms[$name];
  1423. }
  1424. return null;
  1425. }
  1426. /**
  1427. * Retrieve all form subForms/subforms
  1428. *
  1429. * @return array
  1430. */
  1431. public function getSubForms()
  1432. {
  1433. return $this->_subForms;
  1434. }
  1435. /**
  1436. * Remove form subForm/subform
  1437. *
  1438. * @param string $name
  1439. * @return boolean
  1440. */
  1441. public function removeSubForm($name)
  1442. {
  1443. $name = (string) $name;
  1444. if (array_key_exists($name, $this->_subForms)) {
  1445. unset($this->_subForms[$name]);
  1446. if (array_key_exists($name, $this->_order)) {
  1447. unset($this->_order[$name]);
  1448. $this->_orderUpdated = true;
  1449. }
  1450. return true;
  1451. }
  1452. return false;
  1453. }
  1454. /**
  1455. * Remove all form subForms/subforms
  1456. *
  1457. * @return Zend_Form
  1458. */
  1459. public function clearSubForms()
  1460. {
  1461. foreach (array_keys($this->_subForms) as $key) {
  1462. if (array_key_exists($key, $this->_order)) {
  1463. unset($this->_order[$key]);
  1464. }
  1465. }
  1466. $this->_subForms = array();
  1467. $this->_orderUpdated = true;
  1468. return $this;
  1469. }
  1470. // Display groups:
  1471. /**
  1472. * Set default display group class
  1473. *
  1474. * @param string $class
  1475. * @return Zend_Form
  1476. */
  1477. public function setDefaultDisplayGroupClass($class)
  1478. {
  1479. $this->_defaultDisplayGroupClass = (string) $class;
  1480. return $this;
  1481. }
  1482. /**
  1483. * Retrieve default display group class
  1484. *
  1485. * @return string
  1486. */
  1487. public function getDefaultDisplayGroupClass()
  1488. {
  1489. return $this->_defaultDisplayGroupClass;
  1490. }
  1491. /**
  1492. * Add a display group
  1493. *
  1494. * Groups named elements for display purposes.
  1495. *
  1496. * If a referenced element does not yet exist in the form, it is omitted.
  1497. *
  1498. * @param array $elements
  1499. * @param string $name
  1500. * @param array|Zend_Config $options
  1501. * @return Zend_Form
  1502. * @throws Zend_Form_Exception if no valid elements provided
  1503. */
  1504. public function addDisplayGroup(array $elements, $name, $options = null)
  1505. {
  1506. $group = array();
  1507. foreach ($elements as $element) {
  1508. if (isset($this->_elements[$element])) {
  1509. $add = $this->getElement($element);
  1510. if (null !== $add) {
  1511. unset($this->_order[$element]);
  1512. $group[] = $add;
  1513. }
  1514. }
  1515. }
  1516. if (empty($group)) {
  1517. require_once 'Zend/Form/Exception.php';
  1518. throw new Zend_Form_Exception('No valid elements specified for display group');
  1519. }
  1520. $name = (string) $name;
  1521. if (is_array($options)) {
  1522. $options['elements'] = $group;
  1523. } elseif ($options instanceof Zend_Config) {
  1524. $options = $options->toArray();
  1525. $options['elements'] = $group;
  1526. } else {
  1527. $options = array('elements' => $group);
  1528. }
  1529. if (isset($options['displayGroupClass'])) {
  1530. $class = $options['displayGroupClass'];
  1531. unset($options['displayGroupClass']);
  1532. } else {
  1533. $class = $this->getDefaultDisplayGroupClass();
  1534. }
  1535. if (!class_exists($class)) {
  1536. require_once 'Zend/Loader.php';
  1537. Zend_Loader::loadClass($class);
  1538. }
  1539. $this->_displayGroups[$name] = new $class(
  1540. $name,
  1541. $this->getPluginLoader(self::DECORATOR),
  1542. $options
  1543. );
  1544. if (!empty($this->_displayGroupPrefixPaths)) {
  1545. $this->_displayGroups[$name]->addPrefixPaths($this->_displayGroupPrefixPaths);
  1546. }
  1547. $this->_order[$name] = $this->_displayGroups[$name]->getOrder();
  1548. $this->_orderUpdated = true;
  1549. return $this;
  1550. }
  1551. /**
  1552. * Add a display group object (used with cloning)
  1553. *
  1554. * @param Zend_Form_DisplayGroup $group
  1555. * @param string|null $name
  1556. * @return Zend_Form
  1557. */
  1558. protected function _addDisplayGroupObject(Zend_Form_DisplayGroup $group, $name = null)
  1559. {
  1560. if (null === $name) {
  1561. $name = $group->getName();
  1562. if (empty($name)) {
  1563. require_once 'Zend/Form/Exception.php';
  1564. throw new Zend_Form_Exception('Invalid display group added; requires name');
  1565. }
  1566. }
  1567. $this->_displayGroups[$name] = $group;
  1568. if (!empty($this->_displayGroupPrefixPaths)) {
  1569. $this->_displayGroups[$name]->addPrefixPaths($this->_displayGroupPrefixPaths);
  1570. }
  1571. $this->_order[$name] = $this->_displayGroups[$name]->getOrder();
  1572. $this->_orderUpdated = true;
  1573. return $this;
  1574. }
  1575. /**
  1576. * Add multiple display groups at once
  1577. *
  1578. * @param array $groups
  1579. * @return Zend_Form
  1580. */
  1581. public function addDisplayGroups(array $groups)
  1582. {
  1583. foreach ($groups as $key => $spec) {
  1584. $name = null;
  1585. if (!is_numeric($key)) {
  1586. $name = $key;
  1587. }
  1588. if ($spec instanceof Zend_Form_DisplayGroup) {
  1589. $this->_addDisplayGroupObject($spec);
  1590. }
  1591. if (!is_array($spec) || empty($spec)) {
  1592. continue;
  1593. }
  1594. $argc = count($spec);
  1595. $options = array();
  1596. if (isset($spec['elements'])) {
  1597. $elements = $spec['elements'];
  1598. if (isset($spec['name'])) {
  1599. $name = $spec['name'];
  1600. }
  1601. if (isset($spec['options'])) {
  1602. $options = $spec['options'];
  1603. }
  1604. $this->addDisplayGroup($elements, $name, $options);
  1605. } else {
  1606. switch ($argc) {
  1607. case (1 <= $argc):
  1608. $elements = array_shift($spec);
  1609. if (!is_array($elements) && (null !== $name)) {
  1610. $elements = array_merge((array) $elements, $spec);
  1611. $this->addDisplayGroup($elements, $name);
  1612. break;
  1613. }
  1614. case (2 <= $argc):
  1615. if (null !== $name) {
  1616. $options = array_shift($spec);
  1617. $this->addDisplayGroup($elements, $name, $options);
  1618. break;
  1619. }
  1620. $name = array_shift($spec);
  1621. case (3 <= $argc):
  1622. $options = array_shift($spec);
  1623. default:
  1624. $this->addDisplayGroup($elements, $name, $options);
  1625. }
  1626. }
  1627. }
  1628. return $this;
  1629. }
  1630. /**
  1631. * Add multiple display groups (overwrites)
  1632. *
  1633. * @param array $groups
  1634. * @return Zend_Form
  1635. */
  1636. public function setDisplayGroups(array $groups)
  1637. {
  1638. return $this->clearDisplayGroups()
  1639. ->addDisplayGroups($groups);
  1640. }
  1641. /**
  1642. * Return a display group
  1643. *
  1644. * @param string $name
  1645. * @return Zend_Form_DisplayGroup|null
  1646. */
  1647. public function getDisplayGroup($name)
  1648. {
  1649. $name = (string) $name;
  1650. if (isset($this->_displayGroups[$name])) {
  1651. return $this->_displayGroups[$name];
  1652. }
  1653. return null;
  1654. }
  1655. /**
  1656. * Return all display groups
  1657. *
  1658. * @return array
  1659. */
  1660. public function getDisplayGroups()
  1661. {
  1662. return $this->_displayGroups;
  1663. }
  1664. /**
  1665. * Remove a display group by name
  1666. *
  1667. * @param string $name
  1668. * @return boolean
  1669. */
  1670. public function removeDisplayGroup($name)
  1671. {
  1672. $name = (string) $name;
  1673. if (array_key_exists($name, $this->_displayGroups)) {
  1674. foreach ($this->_displayGroups[$name] as $key => $element) {
  1675. if (array_key_exists($key, $this->_elements)) {
  1676. $this->_order[$key] = $element->getOrder();
  1677. $this->_orderUpdated = true;
  1678. }
  1679. }
  1680. unset($this->_displayGroups[$name]);
  1681. if (array_key_exists($name, $this->_order)) {
  1682. unset($this->_order[$name]);
  1683. $this->_orderUpdated = true;
  1684. }
  1685. return true;
  1686. }
  1687. return false;
  1688. }
  1689. /**
  1690. * Remove all display groups
  1691. *
  1692. * @return Zend_Form
  1693. */
  1694. public function clearDisplayGroups()
  1695. {
  1696. foreach ($this->_displayGroups as $key => $group) {
  1697. if (array_key_exists($key, $this->_order)) {
  1698. unset($this->_order[$key]);
  1699. }
  1700. foreach ($group as $name => $element) {
  1701. if (isset($this->_elements[$name])) {
  1702. $this->_order[$name] = $element->getOrder();
  1703. }
  1704. $this->_order[$name] = $element->getOrder();
  1705. }
  1706. }
  1707. $this->_displayGroups = array();
  1708. $this->_orderUpdated = true;
  1709. return $this;
  1710. }
  1711. // Processing
  1712. /**
  1713. * Populate form
  1714. *
  1715. * Proxies to {@link setDefaults()}
  1716. *
  1717. * @param array $values
  1718. * @return Zend_Form
  1719. */
  1720. public function populate(array $values)
  1721. {
  1722. return $this->setDefaults($values);
  1723. }
  1724. /**
  1725. * Determine array key name from given value
  1726. *
  1727. * Given a value such as foo[bar][baz], returns the last element (in this case, 'baz').
  1728. *
  1729. * @param string $value
  1730. * @return string
  1731. */
  1732. protected function _getArrayName($value)
  1733. {
  1734. if (empty($value) || !is_string($value)) {
  1735. return $value;
  1736. }
  1737. if (!strstr($value, '[')) {
  1738. return $value;
  1739. }
  1740. $endPos = strlen($value) - 1;
  1741. if (']' != $value[$endPos]) {
  1742. return $value;
  1743. }
  1744. $start = strrpos($value, '[') + 1;
  1745. $name = substr($value, $start, $endPos - $start);
  1746. return $name;
  1747. }
  1748. /**
  1749. * Extract the value by walking the array using given array path.
  1750. *
  1751. * Given an array path such as foo[bar][baz], returns the value of the last
  1752. * element (in this case, 'baz').
  1753. *
  1754. * @param array $value Array to walk
  1755. * @param string $arrayPath Array notation path of the part to extract
  1756. * @return string
  1757. */
  1758. protected function _dissolveArrayValue($value, $arrayPath)
  1759. {
  1760. // As long as we have more levels
  1761. while ($arrayPos = strpos($arrayPath, '[')) {
  1762. // Get the next key in the path
  1763. $arrayKey = trim(substr($arrayPath, 0, $arrayPos), ']');
  1764. // Set the potentially final value or the next search point in the array
  1765. if (isset($value[$arrayKey])) {
  1766. $value = $value[$arrayKey];
  1767. }
  1768. // Set the next search point in the path
  1769. $arrayPath = trim(substr($arrayPath, $arrayPos + 1), ']');
  1770. }
  1771. if (isset($value[$arrayPath])) {
  1772. $value = $value[$arrayPath];
  1773. }
  1774. return $value;
  1775. }
  1776. /**
  1777. * Converts given arrayPath to an array and attaches given value at the end of it.
  1778. *
  1779. * @param mixed $value The value to attach
  1780. * @param string $arrayPath Given array path to convert and attach to.
  1781. * @return array
  1782. */
  1783. protected function _attachToArray($value, $arrayPath)
  1784. {
  1785. // As long as we have more levels
  1786. while ($arrayPos = strrpos($arrayPath, '[')) {
  1787. // Get the next key in the path
  1788. $arrayKey = trim(substr($arrayPath, $arrayPos + 1), ']');
  1789. // Attach
  1790. $value = array($arrayKey => $value);
  1791. // Set the next search point in the path
  1792. $arrayPath = trim(substr($arrayPath, 0, $arrayPos), ']');
  1793. }
  1794. $value = array($arrayPath => $value);
  1795. return $value;
  1796. }
  1797. /**
  1798. * Validate the form
  1799. *
  1800. * @param array $data
  1801. * @return boolean
  1802. */
  1803. public function isValid($data)
  1804. {
  1805. if (!is_array($data)) {
  1806. require_once 'Zend/Form/Exception.php';
  1807. throw new Zend_Form_Exception(__CLASS__ . '::' . __METHOD__ . ' expects an array');
  1808. }
  1809. $translator = $this->getTranslator();
  1810. $valid = true;
  1811. if ($this->isArray()) {
  1812. $data = $this->_dissolveArrayValue($data, $this->getElementsBelongTo());
  1813. }
  1814. foreach ($this->getElements() as $key => $element) {
  1815. $element->setTranslator($translator);
  1816. if (!isset($data[$key])) {
  1817. $valid = $element->isValid(null, $data) && $valid;
  1818. } else {
  1819. $valid = $element->isValid($data[$key], $data) && $valid;
  1820. }
  1821. }
  1822. foreach ($this->getSubForms() as $key => $form) {
  1823. $form->setTranslator($translator);
  1824. if (isset($data[$key])) {
  1825. $valid = $form->isValid($data[$key]) && $valid;
  1826. } else {
  1827. $valid = $form->isValid($data) && $valid;
  1828. }
  1829. }
  1830. $this->_errorsExist = !$valid;
  1831. // If manually flagged as an error, return invalid status
  1832. if ($this->_errorsForced) {
  1833. return false;
  1834. }
  1835. return $valid;
  1836. }
  1837. /**
  1838. * Validate a partial form
  1839. *
  1840. * Does not check for required flags.
  1841. *
  1842. * @param array $data
  1843. * @return boolean
  1844. */
  1845. public function isValidPartial(array $data)
  1846. {
  1847. if ($this->isArray()) {
  1848. $data = $this->_dissolveArrayValue($data, $this->getElementsBelongTo());
  1849. }
  1850. $translator = $this->getTranslator();
  1851. $valid = true;
  1852. $validatedSubForms = array();
  1853. foreach ($data as $key => $value) {
  1854. if (null !== ($element = $this->getElement($key))) {
  1855. if (null !== $translator) {
  1856. $element->setTranslator($translator);
  1857. }
  1858. $valid = $element->isValid($value, $data) && $valid;
  1859. } elseif (null !== ($subForm = $this->getSubForm($key))) {
  1860. if (null !== $translator) {
  1861. $subForm->setTranslator($translator);
  1862. }
  1863. $valid = $subForm->isValidPartial($data[$key]) && $valid;
  1864. $validatedSubForms[] = $key;
  1865. }
  1866. }
  1867. foreach ($this->getSubForms() as $key => $subForm) {
  1868. if (!in_array($key, $validatedSubForms)) {
  1869. if (null !== $translator) {
  1870. $subForm->setTranslator($translator);
  1871. }
  1872. $valid = $subForm->isValidPartial($data) && $valid;
  1873. }
  1874. }
  1875. $this->_errorsExist = !$valid;
  1876. return $valid;
  1877. }
  1878. /**
  1879. * Process submitted AJAX data
  1880. *
  1881. * Checks if provided $data is valid, via {@link isValidPartial()}. If so,
  1882. * it returns JSON-encoded boolean true. If not, it returns JSON-encoded
  1883. * error messages (as returned by {@link getMessages()}).
  1884. *
  1885. * @param array $data
  1886. * @return string JSON-encoded boolean true or error messages
  1887. */
  1888. public function processAjax(array $data)
  1889. {
  1890. require_once 'Zend/Json.php';
  1891. if ($this->isValidPartial($data)) {
  1892. return Zend_Json::encode(true);
  1893. }
  1894. $messages = $this->getMessages();
  1895. return Zend_Json::encode($messages);
  1896. }
  1897. /**
  1898. * Add a custom error message to return in the event of failed validation
  1899. *
  1900. * @param string $message
  1901. * @return Zend_Form
  1902. */
  1903. public function addErrorMessage($message)
  1904. {
  1905. $this->_errorMessages[] = (string) $message;
  1906. return $this;
  1907. }
  1908. /**
  1909. * Add multiple custom error messages to return in the event of failed validation
  1910. *
  1911. * @param array $messages
  1912. * @return Zend_Form
  1913. */
  1914. public function addErrorMessages(array $messages)
  1915. {
  1916. foreach ($messages as $message) {
  1917. $this->addErrorMessage($message);
  1918. }
  1919. return $this;
  1920. }
  1921. /**
  1922. * Same as addErrorMessages(), but clears custom error message stack first
  1923. *
  1924. * @param array $messages
  1925. * @return Zend_Form
  1926. */
  1927. public function setErrorMessages(array $messages)
  1928. {
  1929. $this->clearErrorMessages();
  1930. return $this->addErrorMessages($messages);
  1931. }
  1932. /**
  1933. * Retrieve custom error messages
  1934. *
  1935. * @return array
  1936. */
  1937. public function getErrorMessages()
  1938. {
  1939. return $this->_errorMessages;
  1940. }
  1941. /**
  1942. * Clear custom error messages stack
  1943. *
  1944. * @return Zend_Form
  1945. */
  1946. public function clearErrorMessages()
  1947. {
  1948. $this->_errorMessages = array();
  1949. return $this;
  1950. }
  1951. /**
  1952. * Mark the element as being in a failed validation state
  1953. *
  1954. * @return Zend_Form
  1955. */
  1956. public function markAsError()
  1957. {
  1958. $this->_errorsExist = true;
  1959. $this->_errorsForced = true;
  1960. return $this;
  1961. }
  1962. /**
  1963. * Add an error message and mark element as failed validation
  1964. *
  1965. * @param string $message
  1966. * @return Zend_Form
  1967. */
  1968. public function addError($message)
  1969. {
  1970. $this->addErrorMessage($message);
  1971. $this->markAsError();
  1972. return $this;
  1973. }
  1974. /**
  1975. * Add multiple error messages and flag element as failed validation
  1976. *
  1977. * @param array $messages
  1978. * @return Zend_Form
  1979. */
  1980. public function addErrors(array $messages)
  1981. {
  1982. foreach ($messages as $message) {
  1983. $this->addError($message);
  1984. }
  1985. return $this;
  1986. }
  1987. /**
  1988. * Overwrite any previously set error messages and flag as failed validation
  1989. *
  1990. * @param array $messages
  1991. * @return Zend_Form
  1992. */
  1993. public function setErrors(array $messages)
  1994. {
  1995. $this->clearErrorMessages();
  1996. return $this->addErrors($messages);
  1997. }
  1998. public function persistData()
  1999. {
  2000. }
  2001. /**
  2002. * Are there errors in the form?
  2003. *
  2004. * @return bool
  2005. */
  2006. public function isErrors()
  2007. {
  2008. return $this->_errorsExist;
  2009. }
  2010. /**
  2011. * Get error codes for all elements failing validation
  2012. *
  2013. * @param string $name
  2014. * @return array
  2015. */
  2016. public function getErrors($name = null)
  2017. {
  2018. $errors = array();
  2019. if ((null !== $name) && isset($this->_elements[$name])) {
  2020. $errors = $this->getElement($name)->getErrors();
  2021. } elseif ((null !== $name) && isset($this->_subForms[$name])) {
  2022. $errors = $this->getSubForm($name)->getErrors();
  2023. } else {
  2024. foreach ($this->_elements as $key => $element) {
  2025. $errors[$key] = $element->getErrors();
  2026. }
  2027. foreach ($this->getSubForms() as $key => $subForm) {
  2028. $fErrors = $this->_attachToArray($subForm->getErrors(), $subForm->getElementsBelongTo());
  2029. $errors = array_merge($errors, $fErrors);
  2030. }
  2031. }
  2032. return $errors;
  2033. }
  2034. /**
  2035. * Retrieve error messages from elements failing validations
  2036. *
  2037. * @param string $name
  2038. * @param bool $suppressArrayNotation
  2039. * @return array
  2040. */
  2041. public function getMessages($name = null, $suppressArrayNotation = false)
  2042. {
  2043. if ((null !== $name) && isset($this->_elements[$name])) {
  2044. return $this->getElement($name)->getMessages();
  2045. }
  2046. if ((null !== $name) && isset($this->_subForms[$name])) {
  2047. return $this->getSubForm($name)->getMessages(null, true);
  2048. }
  2049. $arrayKeys = array();
  2050. foreach ($this->getSubForms() as $key => $subForm) {
  2051. $array = $this->_getArrayName($subForm->getElementsBelongTo());
  2052. if (!empty($array)) {
  2053. if ($name == $array) {
  2054. return $subForm->getMessages(null, true);
  2055. }
  2056. $arrayKeys[$key] = $subForm->getElementsBelongTo();
  2057. }
  2058. }
  2059. $customMessages = $this->_getErrorMessages();
  2060. if ($this->isErrors() && !empty($customMessages)) {
  2061. return $customMessages;
  2062. }
  2063. $messages = array();
  2064. foreach ($this->getElements() as $name => $element) {
  2065. $eMessages = $element->getMessages();
  2066. if (!empty($eMessages)) {
  2067. $messages[$name] = $eMessages;
  2068. }
  2069. }
  2070. foreach ($this->getSubForms() as $key => $subForm) {
  2071. $fMessages = $subForm->getMessages(null, true);
  2072. if (!empty($fMessages)) {
  2073. if (array_key_exists($key, $arrayKeys)) {
  2074. $fMessages = $this->_attachToArray($fMessages, $arrayKeys[$key]);
  2075. $messages = array_merge($messages, $fMessages);
  2076. } else {
  2077. $messages[$key] = $fMessages;
  2078. }
  2079. }
  2080. }
  2081. if (!$suppressArrayNotation && $this->isArray()) {
  2082. $messages = $this->_attachToArray($messages, $this->getElementsBelongTo());
  2083. }
  2084. return $messages;
  2085. }
  2086. // Rendering
  2087. /**
  2088. * Set view object
  2089. *
  2090. * @param Zend_View_Interface $view
  2091. * @return Zend_Form
  2092. */
  2093. public function setView(Zend_View_Interface $view = null)
  2094. {
  2095. $this->_view = $view;
  2096. return $this;
  2097. }
  2098. /**
  2099. * Retrieve view object
  2100. *
  2101. * If none registered, attempts to pull from ViewRenderer.
  2102. *
  2103. * @return Zend_View_Interface|null
  2104. */
  2105. public function getView()
  2106. {
  2107. if (null === $this->_view) {
  2108. require_once 'Zend/Controller/Action/HelperBroker.php';
  2109. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  2110. $this->setView($viewRenderer->view);
  2111. }
  2112. return $this->_view;
  2113. }
  2114. /**
  2115. * Instantiate a decorator based on class name or class name fragment
  2116. *
  2117. * @param string $name
  2118. * @param null|array $options
  2119. * @return Zend_Form_Decorator_Interface
  2120. */
  2121. protected function _getDecorator($name, $options)
  2122. {
  2123. $class = $this->getPluginLoader(self::DECORATOR)->load($name);
  2124. if (null === $options) {
  2125. $decorator = new $class;
  2126. } else {
  2127. $decorator = new $class($options);
  2128. }
  2129. return $decorator;
  2130. }
  2131. /**
  2132. * Add a decorator for rendering the element
  2133. *
  2134. * @param string|Zend_Form_Decorator_Interface $decorator
  2135. * @param array|Zend_Config $options Options with which to initialize decorator
  2136. * @return Zend_Form
  2137. */
  2138. public function addDecorator($decorator, $options = null)
  2139. {
  2140. if ($decorator instanceof Zend_Form_Decorator_Interface) {
  2141. $name = get_class($decorator);
  2142. } elseif (is_string($decorator)) {
  2143. $name = $decorator;
  2144. $decorator = array(
  2145. 'decorator' => $name,
  2146. 'options' => $options,
  2147. );
  2148. } elseif (is_array($decorator)) {
  2149. foreach ($decorator as $name => $spec) {
  2150. break;
  2151. }
  2152. if (is_numeric($name)) {
  2153. require_once 'Zend/Form/Exception.php';
  2154. throw new Zend_Form_Exception('Invalid alias provided to addDecorator; must be alphanumeric string');
  2155. }
  2156. if (is_string($spec)) {
  2157. $decorator = array(
  2158. 'decorator' => $spec,
  2159. 'options' => $options,
  2160. );
  2161. } elseif ($spec instanceof Zend_Form_Decorator_Interface) {
  2162. $decorator = $spec;
  2163. }
  2164. } else {
  2165. require_once 'Zend/Form/Exception.php';
  2166. throw new Zend_Form_Exception('Invalid decorator provided to addDecorator; must be string or Zend_Form_Decorator_Interface');
  2167. }
  2168. $this->_decorators[$name] = $decorator;
  2169. return $this;
  2170. }
  2171. /**
  2172. * Add many decorators at once
  2173. *
  2174. * @param array $decorators
  2175. * @return Zend_Form
  2176. */
  2177. public function addDecorators(array $decorators)
  2178. {
  2179. foreach ($decorators as $decoratorInfo) {
  2180. if (is_string($decoratorInfo)) {
  2181. $this->addDecorator($decoratorInfo);
  2182. } elseif ($decoratorInfo instanceof Zend_Form_Decorator_Interface) {
  2183. $this->addDecorator($decoratorInfo);
  2184. } elseif (is_array($decoratorInfo)) {
  2185. $argc = count($decoratorInfo);
  2186. $options = array();
  2187. if (isset($decoratorInfo['decorator'])) {
  2188. $decorator = $decoratorInfo['decorator'];
  2189. if (isset($decoratorInfo['options'])) {
  2190. $options = $decoratorInfo['options'];
  2191. }
  2192. $this->addDecorator($decorator, $options);
  2193. } else {
  2194. switch (true) {
  2195. case (0 == $argc):
  2196. break;
  2197. case (1 <= $argc):
  2198. $decorator = array_shift($decoratorInfo);
  2199. case (2 <= $argc):
  2200. $options = array_shift($decoratorInfo);
  2201. default:
  2202. $this->addDecorator($decorator, $options);
  2203. break;
  2204. }
  2205. }
  2206. } else {
  2207. require_once 'Zend/Form/Exception.php';
  2208. throw new Zend_Form_Exception('Invalid decorator passed to addDecorators()');
  2209. }
  2210. }
  2211. return $this;
  2212. }
  2213. /**
  2214. * Overwrite all decorators
  2215. *
  2216. * @param array $decorators
  2217. * @return Zend_Form
  2218. */
  2219. public function setDecorators(array $decorators)
  2220. {
  2221. $this->clearDecorators();
  2222. return $this->addDecorators($decorators);
  2223. }
  2224. /**
  2225. * Retrieve a registered decorator
  2226. *
  2227. * @param string $name
  2228. * @return false|Zend_Form_Decorator_Abstract
  2229. */
  2230. public function getDecorator($name)
  2231. {
  2232. if (!isset($this->_decorators[$name])) {
  2233. $len = strlen($name);
  2234. foreach ($this->_decorators as $localName => $decorator) {
  2235. if ($len > strlen($localName)) {
  2236. continue;
  2237. }
  2238. if (0 === substr_compare($localName, $name, -$len, $len, true)) {
  2239. if (is_array($decorator)) {
  2240. return $this->_loadDecorator($decorator, $localName);
  2241. }
  2242. return $decorator;
  2243. }
  2244. }
  2245. return false;
  2246. }
  2247. if (is_array($this->_decorators[$name])) {
  2248. return $this->_loadDecorator($this->_decorators[$name], $name);
  2249. }
  2250. return $this->_decorators[$name];
  2251. }
  2252. /**
  2253. * Retrieve all decorators
  2254. *
  2255. * @return array
  2256. */
  2257. public function getDecorators()
  2258. {
  2259. foreach ($this->_decorators as $key => $value) {
  2260. if (is_array($value)) {
  2261. $this->_loadDecorator($value, $key);
  2262. }
  2263. }
  2264. return $this->_decorators;
  2265. }
  2266. /**
  2267. * Remove a single decorator
  2268. *
  2269. * @param string $name
  2270. * @return bool
  2271. */
  2272. public function removeDecorator($name)
  2273. {
  2274. $decorator = $this->getDecorator($name);
  2275. if ($decorator) {
  2276. if (array_key_exists($name, $this->_decorators)) {
  2277. unset($this->_decorators[$name]);
  2278. } else {
  2279. $class = get_class($decorator);
  2280. if (!array_key_exists($class, $this->_decorators)) {
  2281. return false;
  2282. }
  2283. unset($this->_decorators[$class]);
  2284. }
  2285. return true;
  2286. }
  2287. return false;
  2288. }
  2289. /**
  2290. * Clear all decorators
  2291. *
  2292. * @return Zend_Form
  2293. */
  2294. public function clearDecorators()
  2295. {
  2296. $this->_decorators = array();
  2297. return $this;
  2298. }
  2299. /**
  2300. * Set all element decorators as specified
  2301. *
  2302. * @param array $decorators
  2303. * @param array|null $elements Specific elements to decorate or exclude from decoration
  2304. * @param bool $include Whether $elements is an inclusion or exclusion list
  2305. * @return Zend_Form
  2306. */
  2307. public function setElementDecorators(array $decorators, array $elements = null, $include = true)
  2308. {
  2309. if (is_array($elements)) {
  2310. if ($include) {
  2311. $elementObjs = array();
  2312. foreach ($elements as $name) {
  2313. if (null !== ($element = $this->getElement($name))) {
  2314. $elementObjs[] = $element;
  2315. }
  2316. }
  2317. } else {
  2318. $elementObjs = $this->getElements();
  2319. foreach ($elements as $name) {
  2320. if (array_key_exists($name, $elementObjs)) {
  2321. unset($elementObjs[$name]);
  2322. }
  2323. }
  2324. }
  2325. } else {
  2326. $elementObjs = $this->getElements();
  2327. }
  2328. foreach ($elementObjs as $element) {
  2329. $element->setDecorators($decorators);
  2330. }
  2331. $this->_elementDecorators = $decorators;
  2332. return $this;
  2333. }
  2334. /**
  2335. * Set all display group decorators as specified
  2336. *
  2337. * @param array $decorators
  2338. * @return Zend_Form
  2339. */
  2340. public function setDisplayGroupDecorators(array $decorators)
  2341. {
  2342. foreach ($this->getDisplayGroups() as $group) {
  2343. $group->setDecorators($decorators);
  2344. }
  2345. return $this;
  2346. }
  2347. /**
  2348. * Set all subform decorators as specified
  2349. *
  2350. * @param array $decorators
  2351. * @return Zend_Form
  2352. */
  2353. public function setSubFormDecorators(array $decorators)
  2354. {
  2355. foreach ($this->getSubForms() as $form) {
  2356. $form->setDecorators($decorators);
  2357. }
  2358. return $this;
  2359. }
  2360. /**
  2361. * Render form
  2362. *
  2363. * @param Zend_View_Interface $view
  2364. * @return string
  2365. */
  2366. public function render(Zend_View_Interface $view = null)
  2367. {
  2368. if (null !== $view) {
  2369. $this->setView($view);
  2370. }
  2371. $content = '';
  2372. foreach ($this->getDecorators() as $decorator) {
  2373. $decorator->setElement($this);
  2374. $content = $decorator->render($content);
  2375. }
  2376. return $content;
  2377. }
  2378. /**
  2379. * Serialize as string
  2380. *
  2381. * Proxies to {@link render()}.
  2382. *
  2383. * @return string
  2384. */
  2385. public function __toString()
  2386. {
  2387. try {
  2388. $return = $this->render();
  2389. return $return;
  2390. } catch (Exception $e) {
  2391. $message = "Exception caught by form: " . $e->getMessage()
  2392. . "\nStack Trace:\n" . $e->getTraceAsString();
  2393. trigger_error($message, E_USER_WARNING);
  2394. return '';
  2395. }
  2396. }
  2397. // Localization:
  2398. /**
  2399. * Set translator object
  2400. *
  2401. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  2402. * @return Zend_Form
  2403. */
  2404. public function setTranslator($translator = null)
  2405. {
  2406. if (null === $translator) {
  2407. $this->_translator = null;
  2408. } elseif ($translator instanceof Zend_Translate_Adapter) {
  2409. $this->_translator = $translator;
  2410. } elseif ($translator instanceof Zend_Translate) {
  2411. $this->_translator = $translator->getAdapter();
  2412. } else {
  2413. require_once 'Zend/Form/Exception.php';
  2414. throw new Zend_Form_Exception('Invalid translator specified');
  2415. }
  2416. return $this;
  2417. }
  2418. /**
  2419. * Set global default translator object
  2420. *
  2421. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  2422. * @return void
  2423. */
  2424. public static function setDefaultTranslator($translator = null)
  2425. {
  2426. if (null === $translator) {
  2427. self::$_translatorDefault = null;
  2428. } elseif ($translator instanceof Zend_Translate_Adapter) {
  2429. self::$_translatorDefault = $translator;
  2430. } elseif ($translator instanceof Zend_Translate) {
  2431. self::$_translatorDefault = $translator->getAdapter();
  2432. } else {
  2433. require_once 'Zend/Form/Exception.php';
  2434. throw new Zend_Form_Exception('Invalid translator specified');
  2435. }
  2436. }
  2437. /**
  2438. * Retrieve translator object
  2439. *
  2440. * @return Zend_Translate|null
  2441. */
  2442. public function getTranslator()
  2443. {
  2444. if ($this->translatorIsDisabled()) {
  2445. return null;
  2446. }
  2447. if (null === $this->_translator) {
  2448. return self::getDefaultTranslator();
  2449. }
  2450. return $this->_translator;
  2451. }
  2452. /**
  2453. * Get global default translator object
  2454. *
  2455. * @return null|Zend_Translate
  2456. */
  2457. public static function getDefaultTranslator()
  2458. {
  2459. if (null === self::$_translatorDefault) {
  2460. require_once 'Zend/Registry.php';
  2461. if (Zend_Registry::isRegistered('Zend_Translate')) {
  2462. $translator = Zend_Registry::get('Zend_Translate');
  2463. if ($translator instanceof Zend_Translate_Adapter) {
  2464. return $translator;
  2465. } elseif ($translator instanceof Zend_Translate) {
  2466. return $translator->getAdapter();
  2467. }
  2468. }
  2469. }
  2470. return self::$_translatorDefault;
  2471. }
  2472. /**
  2473. * Indicate whether or not translation should be disabled
  2474. *
  2475. * @param bool $flag
  2476. * @return Zend_Form
  2477. */
  2478. public function setDisableTranslator($flag)
  2479. {
  2480. $this->_translatorDisabled = (bool) $flag;
  2481. return $this;
  2482. }
  2483. /**
  2484. * Is translation disabled?
  2485. *
  2486. * @return bool
  2487. */
  2488. public function translatorIsDisabled()
  2489. {
  2490. return $this->_translatorDisabled;
  2491. }
  2492. /**
  2493. * Overloading: access to elements, form groups, and display groups
  2494. *
  2495. * @param string $name
  2496. * @return Zend_Form_Element|Zend_Form|null
  2497. */
  2498. public function __get($name)
  2499. {
  2500. if (isset($this->_elements[$name])) {
  2501. return $this->_elements[$name];
  2502. } elseif (isset($this->_subForms[$name])) {
  2503. return $this->_subForms[$name];
  2504. } elseif (isset($this->_displayGroups[$name])) {
  2505. return $this->_displayGroups[$name];
  2506. }
  2507. return null;
  2508. }
  2509. /**
  2510. * Overloading: access to elements, form groups, and display groups
  2511. *
  2512. * @param string $name
  2513. * @param Zend_Form_Element|Zend_Form $value
  2514. * @return void
  2515. * @throws Zend_Form_Exception for invalid $value
  2516. */
  2517. public function __set($name, $value)
  2518. {
  2519. if ($value instanceof Zend_Form_Element) {
  2520. $this->addElement($value, $name);
  2521. return;
  2522. } elseif ($value instanceof Zend_Form) {
  2523. $this->addSubForm($value, $name);
  2524. return;
  2525. } elseif (is_array($value)) {
  2526. $this->addDisplayGroup($value, $name);
  2527. return;
  2528. }
  2529. require_once 'Zend/Form/Exception.php';
  2530. if (is_object($value)) {
  2531. $type = get_class($value);
  2532. } else {
  2533. $type = gettype($value);
  2534. }
  2535. throw new Zend_Form_Exception('Only form elements and groups may be overloaded; variable of type "' . $type . '" provided');
  2536. }
  2537. /**
  2538. * Overloading: access to elements, form groups, and display groups
  2539. *
  2540. * @param string $name
  2541. * @return boolean
  2542. */
  2543. public function __isset($name)
  2544. {
  2545. if (isset($this->_elements[$name])
  2546. || isset($this->_subForms[$name])
  2547. || isset($this->_displayGroups[$name]))
  2548. {
  2549. return true;
  2550. }
  2551. return false;
  2552. }
  2553. /**
  2554. * Overloading: access to elements, form groups, and display groups
  2555. *
  2556. * @param string $name
  2557. * @return void
  2558. */
  2559. public function __unset($name)
  2560. {
  2561. if (isset($this->_elements[$name])) {
  2562. unset($this->_elements[$name]);
  2563. } elseif (isset($this->_subForms[$name])) {
  2564. unset($this->_subForms[$name]);
  2565. } elseif (isset($this->_displayGroups[$name])) {
  2566. unset($this->_displayGroups[$name]);
  2567. }
  2568. }
  2569. /**
  2570. * Overloading: allow rendering specific decorators
  2571. *
  2572. * Call renderDecoratorName() to render a specific decorator.
  2573. *
  2574. * @param string $method
  2575. * @param array $args
  2576. * @return string
  2577. * @throws Zend_Form_Exception for invalid decorator or invalid method call
  2578. */
  2579. public function __call($method, $args)
  2580. {
  2581. if ('render' == substr($method, 0, 6)) {
  2582. $decoratorName = substr($method, 6);
  2583. if (false !== ($decorator = $this->getDecorator($decoratorName))) {
  2584. $decorator->setElement($this);
  2585. $seed = '';
  2586. if (0 < count($args)) {
  2587. $seed = array_shift($args);
  2588. }
  2589. return $decorator->render($seed);
  2590. }
  2591. require_once 'Zend/Form/Exception.php';
  2592. throw new Zend_Form_Exception(sprintf('Decorator by name %s does not exist', $decoratorName));
  2593. }
  2594. require_once 'Zend/Form/Exception.php';
  2595. throw new Zend_Form_Exception(sprintf('Method %s does not exist', $method));
  2596. }
  2597. // Interfaces: Iterator, Countable
  2598. /**
  2599. * Current element/subform/display group
  2600. *
  2601. * @return Zend_Form_Element|Zend_Form_DisplayGroup|Zend_Form
  2602. */
  2603. public function current()
  2604. {
  2605. $this->_sort();
  2606. current($this->_order);
  2607. $key = key($this->_order);
  2608. if (isset($this->_elements[$key])) {
  2609. return $this->getElement($key);
  2610. } elseif (isset($this->_subForms[$key])) {
  2611. return $this->getSubForm($key);
  2612. } elseif (isset($this->_displayGroups[$key])) {
  2613. return $this->getDisplayGroup($key);
  2614. } else {
  2615. require_once 'Zend/Form/Exception.php';
  2616. throw new Zend_Form_Exception(sprintf('Corruption detected in form; invalid key ("%s") found in internal iterator', (string) $key));
  2617. }
  2618. }
  2619. /**
  2620. * Current element/subform/display group name
  2621. *
  2622. * @return string
  2623. */
  2624. public function key()
  2625. {
  2626. $this->_sort();
  2627. return key($this->_order);
  2628. }
  2629. /**
  2630. * Move pointer to next element/subform/display group
  2631. *
  2632. * @return void
  2633. */
  2634. public function next()
  2635. {
  2636. $this->_sort();
  2637. next($this->_order);
  2638. }
  2639. /**
  2640. * Move pointer to beginning of element/subform/display group loop
  2641. *
  2642. * @return void
  2643. */
  2644. public function rewind()
  2645. {
  2646. $this->_sort();
  2647. reset($this->_order);
  2648. }
  2649. /**
  2650. * Determine if current element/subform/display group is valid
  2651. *
  2652. * @return bool
  2653. */
  2654. public function valid()
  2655. {
  2656. $this->_sort();
  2657. return (current($this->_order) !== false);
  2658. }
  2659. /**
  2660. * Count of elements/subforms that are iterable
  2661. *
  2662. * @return int
  2663. */
  2664. public function count()
  2665. {
  2666. return count($this->_order);
  2667. }
  2668. /**
  2669. * Set flag to disable loading default decorators
  2670. *
  2671. * @param bool $flag
  2672. * @return Zend_Form
  2673. */
  2674. public function setDisableLoadDefaultDecorators($flag)
  2675. {
  2676. $this->_disableLoadDefaultDecorators = (bool) $flag;
  2677. return $this;
  2678. }
  2679. /**
  2680. * Should we load the default decorators?
  2681. *
  2682. * @return bool
  2683. */
  2684. public function loadDefaultDecoratorsIsDisabled()
  2685. {
  2686. return $this->_disableLoadDefaultDecorators;
  2687. }
  2688. /**
  2689. * Load the default decorators
  2690. *
  2691. * @return void
  2692. */
  2693. public function loadDefaultDecorators()
  2694. {
  2695. if ($this->loadDefaultDecoratorsIsDisabled()) {
  2696. return;
  2697. }
  2698. $decorators = $this->getDecorators();
  2699. if (empty($decorators)) {
  2700. $this->addDecorator('FormElements')
  2701. ->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form'))
  2702. ->addDecorator('Form');
  2703. }
  2704. }
  2705. /**
  2706. * Sort items according to their order
  2707. *
  2708. * @return void
  2709. */
  2710. protected function _sort()
  2711. {
  2712. if ($this->_orderUpdated) {
  2713. $items = array();
  2714. $index = 0;
  2715. foreach ($this->_order as $key => $order) {
  2716. if (null === $order) {
  2717. if (null === ($order = $this->{$key}->getOrder())) {
  2718. while (array_search($index, $this->_order, true)) {
  2719. ++$index;
  2720. }
  2721. $items[$index] = $key;
  2722. ++$index;
  2723. } else {
  2724. $items[$order] = $key;
  2725. }
  2726. } else {
  2727. $items[$order] = $key;
  2728. }
  2729. }
  2730. $items = array_flip($items);
  2731. asort($items);
  2732. $this->_order = $items;
  2733. $this->_orderUpdated = false;
  2734. }
  2735. }
  2736. /**
  2737. * Lazy-load a decorator
  2738. *
  2739. * @param array $decorator Decorator type and options
  2740. * @param mixed $name Decorator name or alias
  2741. * @return Zend_Form_Decorator_Interface
  2742. */
  2743. protected function _loadDecorator(array $decorator, $name)
  2744. {
  2745. $sameName = false;
  2746. if ($name == $decorator['decorator']) {
  2747. $sameName = true;
  2748. }
  2749. $instance = $this->_getDecorator($decorator['decorator'], $decorator['options']);
  2750. if ($sameName) {
  2751. $newName = get_class($instance);
  2752. $decoratorNames = array_keys($this->_decorators);
  2753. $order = array_flip($decoratorNames);
  2754. $order[$newName] = $order[$name];
  2755. $decoratorsExchange = array();
  2756. unset($order[$name]);
  2757. asort($order);
  2758. foreach ($order as $key => $index) {
  2759. if ($key == $newName) {
  2760. $decoratorsExchange[$key] = $instance;
  2761. continue;
  2762. }
  2763. $decoratorsExchange[$key] = $this->_decorators[$key];
  2764. }
  2765. $this->_decorators = $decoratorsExchange;
  2766. } else {
  2767. $this->_decorators[$name] = $instance;
  2768. }
  2769. return $instance;
  2770. }
  2771. /**
  2772. * Retrieve optionally translated custom error messages
  2773. *
  2774. * @return array
  2775. */
  2776. protected function _getErrorMessages()
  2777. {
  2778. $messages = $this->getErrorMessages();
  2779. $translator = $this->getTranslator();
  2780. if (null !== $translator) {
  2781. foreach ($messages as $key => $message) {
  2782. $messages[$key] = $translator->translate($message);
  2783. }
  2784. }
  2785. return $messages;
  2786. }
  2787. }