PageRenderTime 70ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Zend/Form/Element.php

https://github.com/br00ta1/LifeParserWeb
PHP | 2250 lines | 1252 code | 220 blank | 778 comment | 198 complexity | 4f4a4ee67c54dee864ea03ebec925580 MD5 | raw file
Possible License(s): BSD-3-Clause

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

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Form
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** @see Zend_Filter */
  21. require_once 'Zend/Filter.php';
  22. /** @see Zend_Form */
  23. require_once 'Zend/Form.php';
  24. /** @see Zend_Validate_Interface */
  25. require_once 'Zend/Validate/Interface.php';
  26. /** @see Zend_Validate_Abstract */
  27. require_once 'Zend/Validate/Abstract.php';
  28. /**
  29. * Zend_Form_Element
  30. *
  31. * @category Zend
  32. * @package Zend_Form
  33. * @subpackage Element
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @version $Id: Element.php 22464 2010-06-19 17:31:21Z alab $
  37. */
  38. class Zend_Form_Element implements Zend_Validate_Interface
  39. {
  40. /**
  41. * Element Constants
  42. */
  43. const DECORATOR = 'DECORATOR';
  44. const FILTER = 'FILTER';
  45. const VALIDATE = 'VALIDATE';
  46. /**
  47. * Default view helper to use
  48. * @var string
  49. */
  50. public $helper = 'formText';
  51. /**
  52. * 'Allow empty' flag
  53. * @var bool
  54. */
  55. protected $_allowEmpty = true;
  56. /**
  57. * Flag indicating whether or not to insert NotEmpty validator when element is required
  58. * @var bool
  59. */
  60. protected $_autoInsertNotEmptyValidator = true;
  61. /**
  62. * Array to which element belongs
  63. * @var string
  64. */
  65. protected $_belongsTo;
  66. /**
  67. * Element decorators
  68. * @var array
  69. */
  70. protected $_decorators = array();
  71. /**
  72. * Element description
  73. * @var string
  74. */
  75. protected $_description;
  76. /**
  77. * Should we disable loading the default decorators?
  78. * @var bool
  79. */
  80. protected $_disableLoadDefaultDecorators = false;
  81. /**
  82. * Custom error messages
  83. * @var array
  84. */
  85. protected $_errorMessages = array();
  86. /**
  87. * Validation errors
  88. * @var array
  89. */
  90. protected $_errors = array();
  91. /**
  92. * Separator to use when concatenating aggregate error messages (for
  93. * elements having array values)
  94. * @var string
  95. */
  96. protected $_errorMessageSeparator = '; ';
  97. /**
  98. * Element filters
  99. * @var array
  100. */
  101. protected $_filters = array();
  102. /**
  103. * Ignore flag (used when retrieving values at form level)
  104. * @var bool
  105. */
  106. protected $_ignore = false;
  107. /**
  108. * Does the element represent an array?
  109. * @var bool
  110. */
  111. protected $_isArray = false;
  112. /**
  113. * Is the error marked as in an invalid state?
  114. * @var bool
  115. */
  116. protected $_isError = false;
  117. /**
  118. * Has the element been manually marked as invalid?
  119. * @var bool
  120. */
  121. protected $_isErrorForced = false;
  122. /**
  123. * Element label
  124. * @var string
  125. */
  126. protected $_label;
  127. /**
  128. * Plugin loaders for filter and validator chains
  129. * @var array
  130. */
  131. protected $_loaders = array();
  132. /**
  133. * Formatted validation error messages
  134. * @var array
  135. */
  136. protected $_messages = array();
  137. /**
  138. * Element name
  139. * @var string
  140. */
  141. protected $_name;
  142. /**
  143. * Order of element
  144. * @var int
  145. */
  146. protected $_order;
  147. /**
  148. * Required flag
  149. * @var bool
  150. */
  151. protected $_required = false;
  152. /**
  153. * @var Zend_Translate
  154. */
  155. protected $_translator;
  156. /**
  157. * Is translation disabled?
  158. * @var bool
  159. */
  160. protected $_translatorDisabled = false;
  161. /**
  162. * Element type
  163. * @var string
  164. */
  165. protected $_type;
  166. /**
  167. * Array of initialized validators
  168. * @var array Validators
  169. */
  170. protected $_validators = array();
  171. /**
  172. * Array of un-initialized validators
  173. * @var array
  174. */
  175. protected $_validatorRules = array();
  176. /**
  177. * Element value
  178. * @var mixed
  179. */
  180. protected $_value;
  181. /**
  182. * @var Zend_View_Interface
  183. */
  184. protected $_view;
  185. /**
  186. * Is a specific decorator being rendered via the magic renderDecorator()?
  187. *
  188. * This is to allow execution of logic inside the render() methods of child
  189. * elements during the magic call while skipping the parent render() method.
  190. *
  191. * @var bool
  192. */
  193. protected $_isPartialRendering = false;
  194. /**
  195. * Constructor
  196. *
  197. * $spec may be:
  198. * - string: name of element
  199. * - array: options with which to configure element
  200. * - Zend_Config: Zend_Config with options for configuring element
  201. *
  202. * @param string|array|Zend_Config $spec
  203. * @param array|Zend_Config $options
  204. * @return void
  205. * @throws Zend_Form_Exception if no element name after initialization
  206. */
  207. public function __construct($spec, $options = null)
  208. {
  209. if (is_string($spec)) {
  210. $this->setName($spec);
  211. } elseif (is_array($spec)) {
  212. $this->setOptions($spec);
  213. } elseif ($spec instanceof Zend_Config) {
  214. $this->setConfig($spec);
  215. }
  216. if (is_string($spec) && is_array($options)) {
  217. $this->setOptions($options);
  218. } elseif (is_string($spec) && ($options instanceof Zend_Config)) {
  219. $this->setConfig($options);
  220. }
  221. if (null === $this->getName()) {
  222. require_once 'Zend/Form/Exception.php';
  223. throw new Zend_Form_Exception('Zend_Form_Element requires each element to have a name');
  224. }
  225. /**
  226. * Extensions
  227. */
  228. $this->init();
  229. /**
  230. * Register ViewHelper decorator by default
  231. */
  232. $this->loadDefaultDecorators();
  233. }
  234. /**
  235. * Initialize object; used by extending classes
  236. *
  237. * @return void
  238. */
  239. public function init()
  240. {
  241. }
  242. /**
  243. * Set flag to disable loading default decorators
  244. *
  245. * @param bool $flag
  246. * @return Zend_Form_Element
  247. */
  248. public function setDisableLoadDefaultDecorators($flag)
  249. {
  250. $this->_disableLoadDefaultDecorators = (bool) $flag;
  251. return $this;
  252. }
  253. /**
  254. * Should we load the default decorators?
  255. *
  256. * @return bool
  257. */
  258. public function loadDefaultDecoratorsIsDisabled()
  259. {
  260. return $this->_disableLoadDefaultDecorators;
  261. }
  262. /**
  263. * Load default decorators
  264. *
  265. * @return Zend_Form_Element
  266. */
  267. public function loadDefaultDecorators()
  268. {
  269. if ($this->loadDefaultDecoratorsIsDisabled()) {
  270. return $this;
  271. }
  272. $decorators = $this->getDecorators();
  273. if (empty($decorators)) {
  274. $getId = create_function('$decorator',
  275. 'return $decorator->getElement()->getId()
  276. . "-element";');
  277. $this->addDecorator('ViewHelper')
  278. ->addDecorator('Errors')
  279. ->addDecorator('Description', array('tag' => 'p', 'class' => 'description'))
  280. ->addDecorator('HtmlTag', array('tag' => 'dd',
  281. 'id' => array('callback' => $getId)))
  282. ->addDecorator('Label', array('tag' => 'dt'));
  283. }
  284. return $this;
  285. }
  286. /**
  287. * Set object state from options array
  288. *
  289. * @param array $options
  290. * @return Zend_Form_Element
  291. */
  292. public function setOptions(array $options)
  293. {
  294. if (isset($options['prefixPath'])) {
  295. $this->addPrefixPaths($options['prefixPath']);
  296. unset($options['prefixPath']);
  297. }
  298. if (isset($options['disableTranslator'])) {
  299. $this->setDisableTranslator($options['disableTranslator']);
  300. unset($options['disableTranslator']);
  301. }
  302. unset($options['options']);
  303. unset($options['config']);
  304. foreach ($options as $key => $value) {
  305. $method = 'set' . ucfirst($key);
  306. if (in_array($method, array('setTranslator', 'setPluginLoader', 'setView'))) {
  307. if (!is_object($value)) {
  308. continue;
  309. }
  310. }
  311. if (method_exists($this, $method)) {
  312. // Setter exists; use it
  313. $this->$method($value);
  314. } else {
  315. // Assume it's metadata
  316. $this->setAttrib($key, $value);
  317. }
  318. }
  319. return $this;
  320. }
  321. /**
  322. * Set object state from Zend_Config object
  323. *
  324. * @param Zend_Config $config
  325. * @return Zend_Form_Element
  326. */
  327. public function setConfig(Zend_Config $config)
  328. {
  329. return $this->setOptions($config->toArray());
  330. }
  331. // Localization:
  332. /**
  333. * Set translator object for localization
  334. *
  335. * @param Zend_Translate|null $translator
  336. * @return Zend_Form_Element
  337. */
  338. public function setTranslator($translator = null)
  339. {
  340. if (null === $translator) {
  341. $this->_translator = null;
  342. } elseif ($translator instanceof Zend_Translate_Adapter) {
  343. $this->_translator = $translator;
  344. } elseif ($translator instanceof Zend_Translate) {
  345. $this->_translator = $translator->getAdapter();
  346. } else {
  347. require_once 'Zend/Form/Exception.php';
  348. throw new Zend_Form_Exception('Invalid translator specified');
  349. }
  350. return $this;
  351. }
  352. /**
  353. * Retrieve localization translator object
  354. *
  355. * @return Zend_Translate_Adapter|null
  356. */
  357. public function getTranslator()
  358. {
  359. if ($this->translatorIsDisabled()) {
  360. return null;
  361. }
  362. if (null === $this->_translator) {
  363. return Zend_Form::getDefaultTranslator();
  364. }
  365. return $this->_translator;
  366. }
  367. /**
  368. * Does this element have its own specific translator?
  369. *
  370. * @return bool
  371. */
  372. public function hasTranslator()
  373. {
  374. return (bool)$this->_translator;
  375. }
  376. /**
  377. * Indicate whether or not translation should be disabled
  378. *
  379. * @param bool $flag
  380. * @return Zend_Form_Element
  381. */
  382. public function setDisableTranslator($flag)
  383. {
  384. $this->_translatorDisabled = (bool) $flag;
  385. return $this;
  386. }
  387. /**
  388. * Is translation disabled?
  389. *
  390. * @return bool
  391. */
  392. public function translatorIsDisabled()
  393. {
  394. return $this->_translatorDisabled;
  395. }
  396. // Metadata
  397. /**
  398. * Filter a name to only allow valid variable characters
  399. *
  400. * @param string $value
  401. * @param bool $allowBrackets
  402. * @return string
  403. */
  404. public function filterName($value, $allowBrackets = false)
  405. {
  406. $charset = '^a-zA-Z0-9_\x7f-\xff';
  407. if ($allowBrackets) {
  408. $charset .= '\[\]';
  409. }
  410. return preg_replace('/[' . $charset . ']/', '', (string) $value);
  411. }
  412. /**
  413. * Set element name
  414. *
  415. * @param string $name
  416. * @return Zend_Form_Element
  417. */
  418. public function setName($name)
  419. {
  420. $name = $this->filterName($name);
  421. if ('' === $name) {
  422. require_once 'Zend/Form/Exception.php';
  423. throw new Zend_Form_Exception('Invalid name provided; must contain only valid variable characters and be non-empty');
  424. }
  425. $this->_name = $name;
  426. return $this;
  427. }
  428. /**
  429. * Return element name
  430. *
  431. * @return string
  432. */
  433. public function getName()
  434. {
  435. return $this->_name;
  436. }
  437. /**
  438. * Get fully qualified name
  439. *
  440. * Places name as subitem of array and/or appends brackets.
  441. *
  442. * @return string
  443. */
  444. public function getFullyQualifiedName()
  445. {
  446. $name = $this->getName();
  447. if (null !== ($belongsTo = $this->getBelongsTo())) {
  448. $name = $belongsTo . '[' . $name . ']';
  449. }
  450. if ($this->isArray()) {
  451. $name .= '[]';
  452. }
  453. return $name;
  454. }
  455. /**
  456. * Get element id
  457. *
  458. * @return string
  459. */
  460. public function getId()
  461. {
  462. if (isset($this->id)) {
  463. return $this->id;
  464. }
  465. $id = $this->getFullyQualifiedName();
  466. // Bail early if no array notation detected
  467. if (!strstr($id, '[')) {
  468. return $id;
  469. }
  470. // Strip array notation
  471. if ('[]' == substr($id, -2)) {
  472. $id = substr($id, 0, strlen($id) - 2);
  473. }
  474. $id = str_replace('][', '-', $id);
  475. $id = str_replace(array(']', '['), '-', $id);
  476. $id = trim($id, '-');
  477. return $id;
  478. }
  479. /**
  480. * Set element value
  481. *
  482. * @param mixed $value
  483. * @return Zend_Form_Element
  484. */
  485. public function setValue($value)
  486. {
  487. $this->_value = $value;
  488. return $this;
  489. }
  490. /**
  491. * Filter a value
  492. *
  493. * @param string $value
  494. * @param string $key
  495. * @return void
  496. */
  497. protected function _filterValue(&$value, &$key)
  498. {
  499. foreach ($this->getFilters() as $filter) {
  500. $value = $filter->filter($value);
  501. }
  502. }
  503. /**
  504. * Retrieve filtered element value
  505. *
  506. * @return mixed
  507. */
  508. public function getValue()
  509. {
  510. $valueFiltered = $this->_value;
  511. if ($this->isArray() && is_array($valueFiltered)) {
  512. array_walk_recursive($valueFiltered, array($this, '_filterValue'));
  513. } else {
  514. $this->_filterValue($valueFiltered, $valueFiltered);
  515. }
  516. return $valueFiltered;
  517. }
  518. /**
  519. * Retrieve unfiltered element value
  520. *
  521. * @return mixed
  522. */
  523. public function getUnfilteredValue()
  524. {
  525. return $this->_value;
  526. }
  527. /**
  528. * Set element label
  529. *
  530. * @param string $label
  531. * @return Zend_Form_Element
  532. */
  533. public function setLabel($label)
  534. {
  535. $this->_label = (string) $label;
  536. return $this;
  537. }
  538. /**
  539. * Retrieve element label
  540. *
  541. * @return string
  542. */
  543. public function getLabel()
  544. {
  545. $translator = $this->getTranslator();
  546. if (null !== $translator) {
  547. return $translator->translate($this->_label);
  548. }
  549. return $this->_label;
  550. }
  551. /**
  552. * Set element order
  553. *
  554. * @param int $order
  555. * @return Zend_Form_Element
  556. */
  557. public function setOrder($order)
  558. {
  559. $this->_order = (int) $order;
  560. return $this;
  561. }
  562. /**
  563. * Retrieve element order
  564. *
  565. * @return int
  566. */
  567. public function getOrder()
  568. {
  569. return $this->_order;
  570. }
  571. /**
  572. * Set required flag
  573. *
  574. * @param bool $flag Default value is true
  575. * @return Zend_Form_Element
  576. */
  577. public function setRequired($flag = true)
  578. {
  579. $this->_required = (bool) $flag;
  580. return $this;
  581. }
  582. /**
  583. * Is the element required?
  584. *
  585. * @return bool
  586. */
  587. public function isRequired()
  588. {
  589. return $this->_required;
  590. }
  591. /**
  592. * Set flag indicating whether a NotEmpty validator should be inserted when element is required
  593. *
  594. * @param bool $flag
  595. * @return Zend_Form_Element
  596. */
  597. public function setAutoInsertNotEmptyValidator($flag)
  598. {
  599. $this->_autoInsertNotEmptyValidator = (bool) $flag;
  600. return $this;
  601. }
  602. /**
  603. * Get flag indicating whether a NotEmpty validator should be inserted when element is required
  604. *
  605. * @return bool
  606. */
  607. public function autoInsertNotEmptyValidator()
  608. {
  609. return $this->_autoInsertNotEmptyValidator;
  610. }
  611. /**
  612. * Set element description
  613. *
  614. * @param string $description
  615. * @return Zend_Form_Element
  616. */
  617. public function setDescription($description)
  618. {
  619. $this->_description = (string) $description;
  620. return $this;
  621. }
  622. /**
  623. * Retrieve element description
  624. *
  625. * @return string
  626. */
  627. public function getDescription()
  628. {
  629. return $this->_description;
  630. }
  631. /**
  632. * Set 'allow empty' flag
  633. *
  634. * When the allow empty flag is enabled and the required flag is false, the
  635. * element will validate with empty values.
  636. *
  637. * @param bool $flag
  638. * @return Zend_Form_Element
  639. */
  640. public function setAllowEmpty($flag)
  641. {
  642. $this->_allowEmpty = (bool) $flag;
  643. return $this;
  644. }
  645. /**
  646. * Get 'allow empty' flag
  647. *
  648. * @return bool
  649. */
  650. public function getAllowEmpty()
  651. {
  652. return $this->_allowEmpty;
  653. }
  654. /**
  655. * Set ignore flag (used when retrieving values at form level)
  656. *
  657. * @param bool $flag
  658. * @return Zend_Form_Element
  659. */
  660. public function setIgnore($flag)
  661. {
  662. $this->_ignore = (bool) $flag;
  663. return $this;
  664. }
  665. /**
  666. * Get ignore flag (used when retrieving values at form level)
  667. *
  668. * @return bool
  669. */
  670. public function getIgnore()
  671. {
  672. return $this->_ignore;
  673. }
  674. /**
  675. * Set flag indicating if element represents an array
  676. *
  677. * @param bool $flag
  678. * @return Zend_Form_Element
  679. */
  680. public function setIsArray($flag)
  681. {
  682. $this->_isArray = (bool) $flag;
  683. return $this;
  684. }
  685. /**
  686. * Is the element representing an array?
  687. *
  688. * @return bool
  689. */
  690. public function isArray()
  691. {
  692. return $this->_isArray;
  693. }
  694. /**
  695. * Set array to which element belongs
  696. *
  697. * @param string $array
  698. * @return Zend_Form_Element
  699. */
  700. public function setBelongsTo($array)
  701. {
  702. $array = $this->filterName($array, true);
  703. if (!empty($array)) {
  704. $this->_belongsTo = $array;
  705. }
  706. return $this;
  707. }
  708. /**
  709. * Return array name to which element belongs
  710. *
  711. * @return string
  712. */
  713. public function getBelongsTo()
  714. {
  715. return $this->_belongsTo;
  716. }
  717. /**
  718. * Return element type
  719. *
  720. * @return string
  721. */
  722. public function getType()
  723. {
  724. if (null === $this->_type) {
  725. $this->_type = get_class($this);
  726. }
  727. return $this->_type;
  728. }
  729. /**
  730. * Set element attribute
  731. *
  732. * @param string $name
  733. * @param mixed $value
  734. * @return Zend_Form_Element
  735. * @throws Zend_Form_Exception for invalid $name values
  736. */
  737. public function setAttrib($name, $value)
  738. {
  739. $name = (string) $name;
  740. if ('_' == $name[0]) {
  741. require_once 'Zend/Form/Exception.php';
  742. throw new Zend_Form_Exception(sprintf('Invalid attribute "%s"; must not contain a leading underscore', $name));
  743. }
  744. if (null === $value) {
  745. unset($this->$name);
  746. } else {
  747. $this->$name = $value;
  748. }
  749. return $this;
  750. }
  751. /**
  752. * Set multiple attributes at once
  753. *
  754. * @param array $attribs
  755. * @return Zend_Form_Element
  756. */
  757. public function setAttribs(array $attribs)
  758. {
  759. foreach ($attribs as $key => $value) {
  760. $this->setAttrib($key, $value);
  761. }
  762. return $this;
  763. }
  764. /**
  765. * Retrieve element attribute
  766. *
  767. * @param string $name
  768. * @return string
  769. */
  770. public function getAttrib($name)
  771. {
  772. $name = (string) $name;
  773. if (isset($this->$name)) {
  774. return $this->$name;
  775. }
  776. return null;
  777. }
  778. /**
  779. * Return all attributes
  780. *
  781. * @return array
  782. */
  783. public function getAttribs()
  784. {
  785. $attribs = get_object_vars($this);
  786. foreach ($attribs as $key => $value) {
  787. if ('_' == substr($key, 0, 1)) {
  788. unset($attribs[$key]);
  789. }
  790. }
  791. return $attribs;
  792. }
  793. /**
  794. * Overloading: retrieve object property
  795. *
  796. * Prevents access to properties beginning with '_'.
  797. *
  798. * @param string $key
  799. * @return mixed
  800. */
  801. public function __get($key)
  802. {
  803. if ('_' == $key[0]) {
  804. require_once 'Zend/Form/Exception.php';
  805. throw new Zend_Form_Exception(sprintf('Cannot retrieve value for protected/private property "%s"', $key));
  806. }
  807. if (!isset($this->$key)) {
  808. return null;
  809. }
  810. return $this->$key;
  811. }
  812. /**
  813. * Overloading: set object property
  814. *
  815. * @param string $key
  816. * @param mixed $value
  817. * @return voide
  818. */
  819. public function __set($key, $value)
  820. {
  821. $this->setAttrib($key, $value);
  822. }
  823. /**
  824. * Overloading: allow rendering specific decorators
  825. *
  826. * Call renderDecoratorName() to render a specific decorator.
  827. *
  828. * @param string $method
  829. * @param array $args
  830. * @return string
  831. * @throws Zend_Form_Exception for invalid decorator or invalid method call
  832. */
  833. public function __call($method, $args)
  834. {
  835. if ('render' == substr($method, 0, 6)) {
  836. $this->_isPartialRendering = true;
  837. $this->render();
  838. $this->_isPartialRendering = false;
  839. $decoratorName = substr($method, 6);
  840. if (false !== ($decorator = $this->getDecorator($decoratorName))) {
  841. $decorator->setElement($this);
  842. $seed = '';
  843. if (0 < count($args)) {
  844. $seed = array_shift($args);
  845. }
  846. return $decorator->render($seed);
  847. }
  848. require_once 'Zend/Form/Element/Exception.php';
  849. throw new Zend_Form_Element_Exception(sprintf('Decorator by name %s does not exist', $decoratorName));
  850. }
  851. require_once 'Zend/Form/Element/Exception.php';
  852. throw new Zend_Form_Element_Exception(sprintf('Method %s does not exist', $method));
  853. }
  854. // Loaders
  855. /**
  856. * Set plugin loader to use for validator or filter chain
  857. *
  858. * @param Zend_Loader_PluginLoader_Interface $loader
  859. * @param string $type 'decorator', 'filter', or 'validate'
  860. * @return Zend_Form_Element
  861. * @throws Zend_Form_Exception on invalid type
  862. */
  863. public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type)
  864. {
  865. $type = strtoupper($type);
  866. switch ($type) {
  867. case self::DECORATOR:
  868. case self::FILTER:
  869. case self::VALIDATE:
  870. $this->_loaders[$type] = $loader;
  871. return $this;
  872. default:
  873. require_once 'Zend/Form/Exception.php';
  874. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type));
  875. }
  876. }
  877. /**
  878. * Retrieve plugin loader for validator or filter chain
  879. *
  880. * Instantiates with default rules if none available for that type. Use
  881. * 'decorator', 'filter', or 'validate' for $type.
  882. *
  883. * @param string $type
  884. * @return Zend_Loader_PluginLoader
  885. * @throws Zend_Loader_Exception on invalid type.
  886. */
  887. public function getPluginLoader($type)
  888. {
  889. $type = strtoupper($type);
  890. switch ($type) {
  891. case self::FILTER:
  892. case self::VALIDATE:
  893. $prefixSegment = ucfirst(strtolower($type));
  894. $pathSegment = $prefixSegment;
  895. case self::DECORATOR:
  896. if (!isset($prefixSegment)) {
  897. $prefixSegment = 'Form_Decorator';
  898. $pathSegment = 'Form/Decorator';
  899. }
  900. if (!isset($this->_loaders[$type])) {
  901. require_once 'Zend/Loader/PluginLoader.php';
  902. $this->_loaders[$type] = new Zend_Loader_PluginLoader(
  903. array('Zend_' . $prefixSegment . '_' => 'Zend/' . $pathSegment . '/')
  904. );
  905. }
  906. return $this->_loaders[$type];
  907. default:
  908. require_once 'Zend/Form/Exception.php';
  909. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  910. }
  911. }
  912. /**
  913. * Add prefix path for plugin loader
  914. *
  915. * If no $type specified, assumes it is a base path for both filters and
  916. * validators, and sets each according to the following rules:
  917. * - decorators: $prefix = $prefix . '_Decorator'
  918. * - filters: $prefix = $prefix . '_Filter'
  919. * - validators: $prefix = $prefix . '_Validate'
  920. *
  921. * Otherwise, the path prefix is set on the appropriate plugin loader.
  922. *
  923. * @param string $prefix
  924. * @param string $path
  925. * @param string $type
  926. * @return Zend_Form_Element
  927. * @throws Zend_Form_Exception for invalid type
  928. */
  929. public function addPrefixPath($prefix, $path, $type = null)
  930. {
  931. $type = strtoupper($type);
  932. switch ($type) {
  933. case self::DECORATOR:
  934. case self::FILTER:
  935. case self::VALIDATE:
  936. $loader = $this->getPluginLoader($type);
  937. $loader->addPrefixPath($prefix, $path);
  938. return $this;
  939. case null:
  940. $prefix = rtrim($prefix, '_');
  941. $path = rtrim($path, DIRECTORY_SEPARATOR);
  942. foreach (array(self::DECORATOR, self::FILTER, self::VALIDATE) as $type) {
  943. $cType = ucfirst(strtolower($type));
  944. $pluginPath = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR;
  945. $pluginPrefix = $prefix . '_' . $cType;
  946. $loader = $this->getPluginLoader($type);
  947. $loader->addPrefixPath($pluginPrefix, $pluginPath);
  948. }
  949. return $this;
  950. default:
  951. require_once 'Zend/Form/Exception.php';
  952. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  953. }
  954. }
  955. /**
  956. * Add many prefix paths at once
  957. *
  958. * @param array $spec
  959. * @return Zend_Form_Element
  960. */
  961. public function addPrefixPaths(array $spec)
  962. {
  963. if (isset($spec['prefix']) && isset($spec['path'])) {
  964. return $this->addPrefixPath($spec['prefix'], $spec['path']);
  965. }
  966. foreach ($spec as $type => $paths) {
  967. if (is_numeric($type) && is_array($paths)) {
  968. $type = null;
  969. if (isset($paths['prefix']) && isset($paths['path'])) {
  970. if (isset($paths['type'])) {
  971. $type = $paths['type'];
  972. }
  973. $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
  974. }
  975. } elseif (!is_numeric($type)) {
  976. if (!isset($paths['prefix']) || !isset($paths['path'])) {
  977. foreach ($paths as $prefix => $spec) {
  978. if (is_array($spec)) {
  979. foreach ($spec as $path) {
  980. if (!is_string($path)) {
  981. continue;
  982. }
  983. $this->addPrefixPath($prefix, $path, $type);
  984. }
  985. } elseif (is_string($spec)) {
  986. $this->addPrefixPath($prefix, $spec, $type);
  987. }
  988. }
  989. } else {
  990. $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
  991. }
  992. }
  993. }
  994. return $this;
  995. }
  996. // Validation
  997. /**
  998. * Add validator to validation chain
  999. *
  1000. * Note: will overwrite existing validators if they are of the same class.
  1001. *
  1002. * @param string|Zend_Validate_Interface $validator
  1003. * @param bool $breakChainOnFailure
  1004. * @param array $options
  1005. * @return Zend_Form_Element
  1006. * @throws Zend_Form_Exception if invalid validator type
  1007. */
  1008. public function addValidator($validator, $breakChainOnFailure = false, $options = array())
  1009. {
  1010. if ($validator instanceof Zend_Validate_Interface) {
  1011. $name = get_class($validator);
  1012. if (!isset($validator->zfBreakChainOnFailure)) {
  1013. $validator->zfBreakChainOnFailure = $breakChainOnFailure;
  1014. }
  1015. } elseif (is_string($validator)) {
  1016. $name = $validator;
  1017. $validator = array(
  1018. 'validator' => $validator,
  1019. 'breakChainOnFailure' => $breakChainOnFailure,
  1020. 'options' => $options,
  1021. );
  1022. } else {
  1023. require_once 'Zend/Form/Exception.php';
  1024. throw new Zend_Form_Exception('Invalid validator provided to addValidator; must be string or Zend_Validate_Interface');
  1025. }
  1026. $this->_validators[$name] = $validator;
  1027. return $this;
  1028. }
  1029. /**
  1030. * Add multiple validators
  1031. *
  1032. * @param array $validators
  1033. * @return Zend_Form_Element
  1034. */
  1035. public function addValidators(array $validators)
  1036. {
  1037. foreach ($validators as $validatorInfo) {
  1038. if (is_string($validatorInfo)) {
  1039. $this->addValidator($validatorInfo);
  1040. } elseif ($validatorInfo instanceof Zend_Validate_Interface) {
  1041. $this->addValidator($validatorInfo);
  1042. } elseif (is_array($validatorInfo)) {
  1043. $argc = count($validatorInfo);
  1044. $breakChainOnFailure = false;
  1045. $options = array();
  1046. if (isset($validatorInfo['validator'])) {
  1047. $validator = $validatorInfo['validator'];
  1048. if (isset($validatorInfo['breakChainOnFailure'])) {
  1049. $breakChainOnFailure = $validatorInfo['breakChainOnFailure'];
  1050. }
  1051. if (isset($validatorInfo['options'])) {
  1052. $options = $validatorInfo['options'];
  1053. }
  1054. $this->addValidator($validator, $breakChainOnFailure, $options);
  1055. } else {
  1056. switch (true) {
  1057. case (0 == $argc):
  1058. break;
  1059. case (1 <= $argc):
  1060. $validator = array_shift($validatorInfo);
  1061. case (2 <= $argc):
  1062. $breakChainOnFailure = array_shift($validatorInfo);
  1063. case (3 <= $argc):
  1064. $options = array_shift($validatorInfo);
  1065. default:
  1066. $this->addValidator($validator, $breakChainOnFailure, $options);
  1067. break;
  1068. }
  1069. }
  1070. } else {
  1071. require_once 'Zend/Form/Exception.php';
  1072. throw new Zend_Form_Exception('Invalid validator passed to addValidators()');
  1073. }
  1074. }
  1075. return $this;
  1076. }
  1077. /**
  1078. * Set multiple validators, overwriting previous validators
  1079. *
  1080. * @param array $validators
  1081. * @return Zend_Form_Element
  1082. */
  1083. public function setValidators(array $validators)
  1084. {
  1085. $this->clearValidators();
  1086. return $this->addValidators($validators);
  1087. }
  1088. /**
  1089. * Retrieve a single validator by name
  1090. *
  1091. * @param string $name
  1092. * @return Zend_Validate_Interface|false False if not found, validator otherwise
  1093. */
  1094. public function getValidator($name)
  1095. {
  1096. if (!isset($this->_validators[$name])) {
  1097. $len = strlen($name);
  1098. foreach ($this->_validators as $localName => $validator) {
  1099. if ($len > strlen($localName)) {
  1100. continue;
  1101. }
  1102. if (0 === substr_compare($localName, $name, -$len, $len, true)) {
  1103. if (is_array($validator)) {
  1104. return $this->_loadValidator($validator);
  1105. }
  1106. return $validator;
  1107. }
  1108. }
  1109. return false;
  1110. }
  1111. if (is_array($this->_validators[$name])) {
  1112. return $this->_loadValidator($this->_validators[$name]);
  1113. }
  1114. return $this->_validators[$name];
  1115. }
  1116. /**
  1117. * Retrieve all validators
  1118. *
  1119. * @return array
  1120. */
  1121. public function getValidators()
  1122. {
  1123. $validators = array();
  1124. foreach ($this->_validators as $key => $value) {
  1125. if ($value instanceof Zend_Validate_Interface) {
  1126. $validators[$key] = $value;
  1127. continue;
  1128. }
  1129. $validator = $this->_loadValidator($value);
  1130. $validators[get_class($validator)] = $validator;
  1131. }
  1132. return $validators;
  1133. }
  1134. /**
  1135. * Remove a single validator by name
  1136. *
  1137. * @param string $name
  1138. * @return bool
  1139. */
  1140. public function removeValidator($name)
  1141. {
  1142. if (isset($this->_validators[$name])) {
  1143. unset($this->_validators[$name]);
  1144. } else {
  1145. $len = strlen($name);
  1146. foreach (array_keys($this->_validators) as $validator) {
  1147. if ($len > strlen($validator)) {
  1148. continue;
  1149. }
  1150. if (0 === substr_compare($validator, $name, -$len, $len, true)) {
  1151. unset($this->_validators[$validator]);
  1152. break;
  1153. }
  1154. }
  1155. }
  1156. return $this;
  1157. }
  1158. /**
  1159. * Clear all validators
  1160. *
  1161. * @return Zend_Form_Element
  1162. */
  1163. public function clearValidators()
  1164. {
  1165. $this->_validators = array();
  1166. return $this;
  1167. }
  1168. /**
  1169. * Validate element value
  1170. *
  1171. * If a translation adapter is registered, any error messages will be
  1172. * translated according to the current locale, using the given error code;
  1173. * if no matching translation is found, the original message will be
  1174. * utilized.
  1175. *
  1176. * Note: The *filtered* value is validated.
  1177. *
  1178. * @param mixed $value
  1179. * @param mixed $context
  1180. * @return boolean
  1181. */
  1182. public function isValid($value, $context = null)
  1183. {
  1184. $this->setValue($value);
  1185. $value = $this->getValue();
  1186. if ((('' === $value) || (null === $value))
  1187. && !$this->isRequired()
  1188. && $this->getAllowEmpty()
  1189. ) {
  1190. return true;
  1191. }
  1192. if ($this->isRequired()
  1193. && $this->autoInsertNotEmptyValidator()
  1194. && !$this->getValidator('NotEmpty'))
  1195. {
  1196. $validators = $this->getValidators();
  1197. $notEmpty = array('validator' => 'NotEmpty', 'breakChainOnFailure' => true);
  1198. array_unshift($validators, $notEmpty);
  1199. $this->setValidators($validators);
  1200. }
  1201. // Find the correct translator. Zend_Validate_Abstract::getDefaultTranslator()
  1202. // will get either the static translator attached to Zend_Validate_Abstract
  1203. // or the 'Zend_Translate' from Zend_Registry.
  1204. if (Zend_Validate_Abstract::hasDefaultTranslator() &&
  1205. !Zend_Form::hasDefaultTranslator())
  1206. {
  1207. $translator = Zend_Validate_Abstract::getDefaultTranslator();
  1208. if ($this->hasTranslator()) {
  1209. // only pick up this element's translator if it was attached directly.
  1210. $translator = $this->getTranslator();
  1211. }
  1212. } else {
  1213. $translator = $this->getTranslator();
  1214. }
  1215. $this->_messages = array();
  1216. $this->_errors = array();
  1217. $result = true;
  1218. $isArray = $this->isArray();
  1219. foreach ($this->getValidators() as $key => $validator) {
  1220. if (method_exists($validator, 'setTranslator')) {
  1221. if (method_exists($validator, 'hasTranslator')) {
  1222. if (!$validator->hasTranslator()) {
  1223. $validator->setTranslator($translator);
  1224. }
  1225. } else {
  1226. $validator->setTranslator($translator);
  1227. }
  1228. }
  1229. if (method_exists($validator, 'setDisableTranslator')) {
  1230. $validator->setDisableTranslator($this->translatorIsDisabled());
  1231. }
  1232. if ($isArray && is_array($value)) {
  1233. $messages = array();
  1234. $errors = array();
  1235. foreach ($value as $val) {
  1236. if (!$validator->isValid($val, $context)) {
  1237. $result = false;
  1238. if ($this->_hasErrorMessages()) {
  1239. $messages = $this->_getErrorMessages();
  1240. $errors = $messages;
  1241. } else {
  1242. $messages = array_merge($messages, $validator->getMessages());
  1243. $errors = array_merge($errors, $validator->getErrors());
  1244. }
  1245. }
  1246. }
  1247. if ($result) {
  1248. continue;
  1249. }
  1250. } elseif ($validator->isValid($value, $context)) {
  1251. continue;
  1252. } else {
  1253. $result = false;
  1254. if ($this->_hasErrorMessages()) {
  1255. $messages = $this->_getErrorMessages();
  1256. $errors = $messages;
  1257. } else {
  1258. $messages = $validator->getMessages();
  1259. $errors = array_keys($messages);
  1260. }
  1261. }
  1262. $result = false;
  1263. $this->_messages = array_merge($this->_messages, $messages);
  1264. $this->_errors = array_merge($this->_errors, $errors);
  1265. if ($validator->zfBreakChainOnFailure) {
  1266. break;
  1267. }
  1268. }
  1269. // If element manually flagged as invalid, return false
  1270. if ($this->_isErrorForced) {
  1271. return false;
  1272. }
  1273. return $result;
  1274. }
  1275. /**
  1276. * Add a custom error message to return in the event of failed validation
  1277. *
  1278. * @param string $message
  1279. * @return Zend_Form_Element
  1280. */
  1281. public function addErrorMessage($message)
  1282. {
  1283. $this->_errorMessages[] = (string) $message;
  1284. return $this;
  1285. }
  1286. /**
  1287. * Add multiple custom error messages to return in the event of failed validation
  1288. *
  1289. * @param array $messages
  1290. * @return Zend_Form_Element
  1291. */
  1292. public function addErrorMessages(array $messages)
  1293. {
  1294. foreach ($messages as $message) {
  1295. $this->addErrorMessage($message);
  1296. }
  1297. return $this;
  1298. }
  1299. /**
  1300. * Same as addErrorMessages(), but clears custom error message stack first
  1301. *
  1302. * @param array $messages
  1303. * @return Zend_Form_Element
  1304. */
  1305. public function setErrorMessages(array $messages)
  1306. {
  1307. $this->clearErrorMessages();
  1308. return $this->addErrorMessages($messages);
  1309. }
  1310. /**
  1311. * Retrieve custom error messages
  1312. *
  1313. * @return array
  1314. */
  1315. public function getErrorMessages()
  1316. {
  1317. return $this->_errorMessages;
  1318. }
  1319. /**
  1320. * Clear custom error messages stack
  1321. *
  1322. * @return Zend_Form_Element
  1323. */
  1324. public function clearErrorMessages()
  1325. {
  1326. $this->_errorMessages = array();
  1327. return $this;
  1328. }
  1329. /**
  1330. * Get errorMessageSeparator
  1331. *
  1332. * @return string
  1333. */
  1334. public function getErrorMessageSeparator()
  1335. {
  1336. return $this->_errorMessageSeparator;
  1337. }
  1338. /**
  1339. * Set errorMessageSeparator
  1340. *
  1341. * @param string $separator
  1342. * @return Zend_Form_Element
  1343. */
  1344. public function setErrorMessageSeparator($separator)
  1345. {
  1346. $this->_errorMessageSeparator = $separator;
  1347. return $this;
  1348. }
  1349. /**
  1350. * Mark the element as being in a failed validation state
  1351. *
  1352. * @return Zend_Form_Element
  1353. */
  1354. public function markAsError()
  1355. {
  1356. $messages = $this->getMessages();
  1357. $customMessages = $this->_getErrorMessages();
  1358. $messages = $messages + $customMessages;
  1359. if (empty($messages)) {
  1360. $this->_isError = true;
  1361. } else {
  1362. $this->_messages = $messages;
  1363. }
  1364. $this->_isErrorForced = true;
  1365. return $this;
  1366. }
  1367. /**
  1368. * Add an error message and mark element as failed validation
  1369. *
  1370. * @param string $message
  1371. * @return Zend_Form_Element
  1372. */
  1373. public function addError($message)
  1374. {
  1375. $this->addErrorMessage($message);
  1376. $this->markAsError();
  1377. return $this;
  1378. }
  1379. /**
  1380. * Add multiple error messages and flag element as failed validation
  1381. *
  1382. * @param array $messages
  1383. * @return Zend_Form_Element
  1384. */
  1385. public function addErrors(array $messages)
  1386. {
  1387. foreach ($messages as $message) {
  1388. $this->addError($message);
  1389. }
  1390. return $this;
  1391. }
  1392. /**
  1393. * Overwrite any previously set error messages and flag as failed validation
  1394. *
  1395. * @param array $messages
  1396. * @return Zend_Form_Element
  1397. */
  1398. public function setErrors(array $messages)
  1399. {
  1400. $this->clearErrorMessages();
  1401. return $this->addErrors($messages);
  1402. }
  1403. /**
  1404. * Are there errors registered?
  1405. *
  1406. * @return bool
  1407. */
  1408. public function hasErrors()
  1409. {
  1410. return (!empty($this->_messages) || $this->_isError);
  1411. }
  1412. /**
  1413. * Retrieve validator chain errors
  1414. *
  1415. * @return array
  1416. */
  1417. public function getErrors()
  1418. {
  1419. return $this->_errors;
  1420. }
  1421. /**
  1422. * Retrieve error messages
  1423. *
  1424. * @return array
  1425. */
  1426. public function getMessages()
  1427. {
  1428. return $this->_messages;
  1429. }
  1430. // Filtering
  1431. /**
  1432. * Add a filter to the element
  1433. *
  1434. * @param string|Zend_Filter_Interface $filter
  1435. * @return Zend_Form_Element
  1436. */
  1437. public function addFilter($filter, $options = array())
  1438. {
  1439. if ($filter instanceof Zend_Filter_Interface) {
  1440. $name = get_class($filter);
  1441. } elseif (is_string($filter)) {
  1442. $name = $filter;
  1443. $filter = array(
  1444. 'filter' => $filter,
  1445. 'options' => $options,
  1446. );
  1447. $this->_filters[$name] = $filter;
  1448. } else {
  1449. require_once 'Zend/Form/Exception.php';
  1450. throw new Zend_Form_Exception('Invalid filter provided to addFilter; must be string or Zend_Filter_Interface');
  1451. }
  1452. $this->_filters[$name] = $filter;
  1453. return $this;
  1454. }
  1455. /**
  1456. * Add filters to element
  1457. *
  1458. * @param array $filters
  1459. * @return Zend_Form_Element
  1460. */
  1461. public function addFilters(array $filters)
  1462. {
  1463. foreach ($filters as $filterInfo) {
  1464. if (is_string($filterInfo)) {
  1465. $this->addFilter($filterInfo);
  1466. } elseif ($filterInfo instanceof Zend_Filter_Interface) {
  1467. $this->addFilter($filterInfo);
  1468. } elseif (is_array($filterInfo)) {
  1469. $argc = count($filterInfo);
  1470. $options = array();
  1471. if (isset($filterInfo['filter'])) {
  1472. $filter = $filterInfo['filter'];
  1473. if (isset($filterInfo['options'])) {
  1474. $options = $filterInfo['options'];
  1475. }
  1476. $this->addFilter($filter, $options);
  1477. } else {
  1478. switch (true) {
  1479. case (0 == $argc):
  1480. break;
  1481. case (1 <= $argc):
  1482. $filter = array_shift($filterInfo);
  1483. case (2 <= $argc):
  1484. $options = array_shift($filterInfo);
  1485. default:
  1486. $this->addFilter($filter, $options);
  1487. break;
  1488. }
  1489. }
  1490. } else {
  1491. require_once 'Zend/Form/Exception.php';
  1492. throw new Zend_Form_Exception('Invalid filter passed to addFilters()');
  1493. }
  1494. }
  1495. return $this;
  1496. }
  1497. /**
  1498. * Add filters to element, overwriting any already existing
  1499. *
  1500. * @param array $filters
  1501. * @return Zend_Form_Element
  1502. */
  1503. public function setFilters(array $filters)
  1504. {
  1505. $this->clearFilters();
  1506. return $this->addFilters($filters);
  1507. }
  1508. /**
  1509. * Retrieve a single filter by name
  1510. *
  1511. * @param string $name
  1512. * @return Zend_Filter_Interface
  1513. */
  1514. public function getFilter($name)
  1515. {
  1516. if (!isset($this->_filters[$name])) {
  1517. $len = strlen($name);
  1518. foreach ($this->_filters as $localName => $filter) {
  1519. if ($len > strlen($localName)) {
  1520. continue;
  1521. }
  1522. if (0 === substr_compare($localName, $name, -$len, $len, true)) {
  1523. if (is_array($filter)) {
  1524. return $this->_loadFilter($filter);
  1525. }
  1526. return $filter;
  1527. }
  1528. }
  1529. return false;
  1530. }
  1531. if (is_array($this->_filters[$name])) {
  1532. return $this->_loadFilter($this->_filters[$name]);
  1533. }
  1534. return $this->_filters[$name];
  1535. }
  1536. /**
  1537. * Get all filters
  1538. *
  1539. * @return array
  1540. */
  1541. public function getFilters()
  1542. {
  1543. $filters = array();
  1544. foreach ($this->_filters as $key => $value) {
  1545. if ($value instanceof Zend_Filter_Interface) {
  1546. $filters[$key] = $value;
  1547. continue;
  1548. }
  1549. $filter = $this->_loadFilter($value);
  1550. $filters[get_class($filter)] = $filter;
  1551. }
  1552. return $filters;
  1553. }
  1554. /**
  1555. * Remove a filter by name
  1556. *
  1557. * @param string $name
  1558. * @return Zend_Form_Element
  1559. */
  1560. public function removeFilter($name)
  1561. {
  1562. if (isset($this->_filters[$name])) {
  1563. unset($this->_filters[$name]);
  1564. } else {
  1565. $len = strlen($name);
  1566. foreach (array_keys($this->_filters) as $filter) {
  1567. if ($len > strlen($filter)) {
  1568. continue;
  1569. }
  1570. if (0 === substr_compare($filter, $name, -$len, $len, true)) {
  1571. unset($this->_filters[$filter]);
  1572. break;
  1573. }
  1574. }
  1575. }
  1576. return $this;
  1577. }
  1578. /**
  1579. * Clear all filters
  1580. *
  1581. * @return Zend_Form_Element
  1582. */
  1583. public function clearFilters()
  1584. {
  1585. $this->_filters = array();
  1586. return $this;
  1587. }
  1588. // Rendering
  1589. /**
  1590. * Set view object
  1591. *
  1592. * @param Zend_View_Interface $view
  1593. * @return Zend_Form_Element
  1594. */
  1595. public function setView(Zend_View_Interface $view = null)
  1596. {
  1597. $this->_view = $view;
  1598. return $this;
  1599. }
  1600. /**
  1601. * Retrieve view object
  1602. *
  1603. * Retrieves from ViewRenderer if none previously set.
  1604. *
  1605. * @return null|Zend_View_Interface
  1606. */
  1607. public function getView()
  1608. {
  1609. if (null === $this->_view) {
  1610. require_once 'Zend/Controller/Action/HelperBroker.php';
  1611. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  1612. $this->setView($viewRenderer->view);
  1613. }
  1614. return $this->_view;
  1615. }
  1616. /**
  1617. * Instantiate a decorator based on class name or class name fragment
  1618. *
  1619. * @param string $name
  1620. * @param null|array $options
  1621. * @return Zend_Form_Decorator_Interface
  1622. */
  1623. protected function _getDecorator($name, $options)
  1624. {
  1625. $class = $this->getPluginLoader(self::DECORATOR)->load($name);
  1626. if (null === $options) {
  1627. $decorator = new $class;
  1628. } else {
  1629. $decorator = new $class($options);
  1630. }
  1631. return $decorator;
  1632. }
  1633. /**
  1634. * Add a decorator for rendering the element
  1635. *
  1636. * @param string|Zend_Form_Decorator_Interface $decorator
  1637. * @param array|Zend_Config $options Options with which to initialize decorator
  1638. * @return Zend_Form_Element
  1639. */
  1640. public function addDecorator($decorator, $options = null)
  1641. {
  1642. if ($decorator instanceof Zend_Form_Decorator_Interface) {
  1643. $name = get_class($decorator);
  1644. } elseif (is_string($decorator)) {
  1645. $name = $decorator;
  1646. $decorator = array(
  1647. 'decorator' => $name,
  1648. 'options' => $options,
  1649. );
  1650. } elseif (is_array($decorator)) {
  1651. foreach ($decorator as $name => $spec) {
  1652. break;
  1653. }
  1654. if (is_numeric($name)) {
  1655. require_once 'Zend/Form/Exception.php';
  1656. throw new Zend_Form_Exception('Invalid alias provided to addDecorator; must be alphanumeric string');
  1657. }
  1658. if (is_string($spec)) {
  1659. $decorator = array(
  1660. 'decorator' => $spec,
  1661. 'options' => $options,
  1662. );
  1663. } elseif ($spec instanceof Zend_Form_Decorator_Interface) {
  1664. $decorator = $spec;
  1665. }
  1666. } else {
  1667. require_once 'Zend/Form/Exception.php';
  1668. throw new Zend_Form_Exception('Invalid decorator provided to addDecorator; must be string or Zend_Form_Decorator_Interface');
  1669. }
  1670. $this->_decorators[$name] = $decorator;
  1671. return $

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