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

/application/libraries/Zend/Form/Element.php

https://bitbucket.org/FnTm/codeigniter-zend-sample-application
PHP | 2247 lines | 1249 code | 220 blank | 778 comment | 198 complexity | 840e659c0ad424060c7b8cfaf0144202 MD5 | raw file

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

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

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