PageRenderTime 74ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/protected/vendors/Zend/Form/Element.php

https://bitbucket.org/negge/tlklan2
PHP | 2271 lines | 1264 code | 221 blank | 786 comment | 202 complexity | 8098c1a60586636b5e533c56e62f1112 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, BSD-2-Clause, GPL-3.0

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

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