PageRenderTime 91ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/Form.php

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