PageRenderTime 68ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/Form/Element.php

https://bitbucket.org/winponta/zend
PHP | 2269 lines | 1262 code | 221 blank | 786 comment | 202 complexity | bf5b8380dbc8d535781554973ee96eb1 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-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 25173 2012-12-22 20:05:32Z 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) . $nsSeparator;
  954. $path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  955. foreach (array(self::DECORATOR, self::FILTER, self::VALIDATE) as $type) {
  956. $cType = ucfirst(strtolower($type));
  957. $loader = $this->getPluginLoader($type);
  958. $loader->addPrefixPath($prefix . $cType, $path . $cType . DIRECTORY_SEPARATOR);
  959. }
  960. return $this;
  961. default:
  962. require_once 'Zend/Form/Exception.php';
  963. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  964. }
  965. }
  966. /**
  967. * Add many prefix paths at once
  968. *
  969. * @param array $spec
  970. * @return Zend_Form_Element
  971. */
  972. public function addPrefixPaths(array $spec)
  973. {
  974. if (isset($spec['prefix']) && isset($spec['path'])) {
  975. return $this->addPrefixPath($spec['prefix'], $spec['path']);
  976. }
  977. foreach ($spec as $type => $paths) {
  978. if (is_numeric($type) && is_array($paths)) {
  979. $type = null;
  980. if (isset($paths['prefix']) && isset($paths['path'])) {
  981. if (isset($paths['type'])) {
  982. $type = $paths['type'];
  983. }
  984. $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
  985. }
  986. } elseif (!is_numeric($type)) {
  987. if (!isset($paths['prefix']) || !isset($paths['path'])) {
  988. foreach ($paths as $prefix => $spec) {
  989. if (is_array($spec)) {
  990. foreach ($spec as $path) {
  991. if (!is_string($path)) {
  992. continue;
  993. }
  994. $this->addPrefixPath($prefix, $path, $type);
  995. }
  996. } elseif (is_string($spec)) {
  997. $this->addPrefixPath($prefix, $spec, $type);
  998. }
  999. }
  1000. } else {
  1001. $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
  1002. }
  1003. }
  1004. }
  1005. return $this;
  1006. }
  1007. // Validation
  1008. /**
  1009. * Add validator to validation chain
  1010. *
  1011. * Note: will overwrite existing validators if they are of the same class.
  1012. *
  1013. * @param string|Zend_Validate_Interface $validator
  1014. * @param bool $breakChainOnFailure
  1015. * @param array $options
  1016. * @return Zend_Form_Element
  1017. * @throws Zend_Form_Exception if invalid validator type
  1018. */
  1019. public function addValidator($validator, $breakChainOnFailure = false, $options = array())
  1020. {
  1021. if ($validator instanceof Zend_Validate_Interface) {
  1022. $name = get_class($validator);
  1023. if (!isset($validator->zfBreakChainOnFailure)) {
  1024. $validator->zfBreakChainOnFailure = $breakChainOnFailure;
  1025. }
  1026. } elseif (is_string($validator)) {
  1027. $name = $validator;
  1028. $validator = array(
  1029. 'validator' => $validator,
  1030. 'breakChainOnFailure' => $breakChainOnFailure,
  1031. 'options' => $options,
  1032. );
  1033. } else {
  1034. require_once 'Zend/Form/Exception.php';
  1035. throw new Zend_Form_Exception('Invalid validator provided to addValidator; must be string or Zend_Validate_Interface');
  1036. }
  1037. $this->_validators[$name] = $validator;
  1038. return $this;
  1039. }
  1040. /**
  1041. * Add multiple validators
  1042. *
  1043. * @param array $validators
  1044. * @return Zend_Form_Element
  1045. */
  1046. public function addValidators(array $validators)
  1047. {
  1048. foreach ($validators as $validatorInfo) {
  1049. if (is_string($validatorInfo)) {
  1050. $this->addValidator($validatorInfo);
  1051. } elseif ($validatorInfo instanceof Zend_Validate_Interface) {
  1052. $this->addValidator($validatorInfo);
  1053. } elseif (is_array($validatorInfo)) {
  1054. $argc = count($validatorInfo);
  1055. $breakChainOnFailure = false;
  1056. $options = array();
  1057. if (isset($validatorInfo['validator'])) {
  1058. $validator = $validatorInfo['validator'];
  1059. if (isset($validatorInfo['breakChainOnFailure'])) {
  1060. $breakChainOnFailure = $validatorInfo['breakChainOnFailure'];
  1061. }
  1062. if (isset($validatorInfo['options'])) {
  1063. $options = $validatorInfo['options'];
  1064. }
  1065. $this->addValidator($validator, $breakChainOnFailure, $options);
  1066. } else {
  1067. switch (true) {
  1068. case (0 == $argc):
  1069. break;
  1070. case (1 <= $argc):
  1071. $validator = array_shift($validatorInfo);
  1072. case (2 <= $argc):
  1073. $breakChainOnFailure = array_shift($validatorInfo);
  1074. case (3 <= $argc):
  1075. $options = array_shift($validatorInfo);
  1076. default:
  1077. $this->addValidator($validator, $breakChainOnFailure, $options);
  1078. break;
  1079. }
  1080. }
  1081. } else {
  1082. require_once 'Zend/Form/Exception.php';
  1083. throw new Zend_Form_Exception('Invalid validator passed to addValidators()');
  1084. }
  1085. }
  1086. return $this;
  1087. }
  1088. /**
  1089. * Set multiple validators, overwriting previous validators
  1090. *
  1091. * @param array $validators
  1092. * @return Zend_Form_Element
  1093. */
  1094. public function setValidators(array $validators)
  1095. {
  1096. $this->clearValidators();
  1097. return $this->addValidators($validators);
  1098. }
  1099. /**
  1100. * Retrieve a single validator by name
  1101. *
  1102. * @param string $name
  1103. * @return Zend_Validate_Interface|false False if not found, validator otherwise
  1104. */
  1105. public function getValidator($name)
  1106. {
  1107. if (!isset($this->_validators[$name])) {
  1108. $len = strlen($name);
  1109. foreach ($this->_validators as $localName => $validator) {
  1110. if ($len > strlen($localName)) {
  1111. continue;
  1112. }
  1113. if (0 === substr_compare($localName, $name, -$len, $len, true)) {
  1114. if (is_array($validator)) {
  1115. return $this->_loadValidator($validator);
  1116. }
  1117. return $validator;
  1118. }
  1119. }
  1120. return false;
  1121. }
  1122. if (is_array($this->_validators[$name])) {
  1123. return $this->_loadValidator($this->_validators[$name]);
  1124. }
  1125. return $this->_validators[$name];
  1126. }
  1127. /**
  1128. * Retrieve all validators
  1129. *
  1130. * @return array
  1131. */
  1132. public function getValidators()
  1133. {
  1134. $validators = array();
  1135. foreach ($this->_validators as $key => $value) {
  1136. if ($value instanceof Zend_Validate_Interface) {
  1137. $validators[$key] = $value;
  1138. continue;
  1139. }
  1140. $validator = $this->_loadValidator($value);
  1141. $validators[get_class($validator)] = $validator;
  1142. }
  1143. return $validators;
  1144. }
  1145. /**
  1146. * Remove a single validator by name
  1147. *
  1148. * @param string $name
  1149. * @return bool
  1150. */
  1151. public function removeValidator($name)
  1152. {
  1153. if (isset($this->_validators[$name])) {
  1154. unset($this->_validators[$name]);
  1155. } else {
  1156. $len = strlen($name);
  1157. foreach (array_keys($this->_validators) as $validator) {
  1158. if ($len > strlen($validator)) {
  1159. continue;
  1160. }
  1161. if (0 === substr_compare($validator, $name, -$len, $len, true)) {
  1162. unset($this->_validators[$validator]);
  1163. break;
  1164. }
  1165. }
  1166. }
  1167. return $this;
  1168. }
  1169. /**
  1170. * Clear all validators
  1171. *
  1172. * @return Zend_Form_Element
  1173. */
  1174. public function clearValidators()
  1175. {
  1176. $this->_validators = array();
  1177. return $this;
  1178. }
  1179. /**
  1180. * Validate element value
  1181. *
  1182. * If a translation adapter is registered, any error messages will be
  1183. * translated according to the current locale, using the given error code;
  1184. * if no matching translation is found, the original message will be
  1185. * utilized.
  1186. *
  1187. * Note: The *filtered* value is validated.
  1188. *
  1189. * @param mixed $value
  1190. * @param mixed $context
  1191. * @return boolean
  1192. */
  1193. public function isValid($value, $context = null)
  1194. {
  1195. $this->setValue($value);
  1196. $value = $this->getValue();
  1197. if ((('' === $value) || (null === $value))
  1198. && !$this->isRequired()
  1199. && $this->getAllowEmpty()
  1200. ) {
  1201. return true;
  1202. }
  1203. if ($this->isRequired()
  1204. && $this->autoInsertNotEmptyValidator()
  1205. && !$this->getValidator('NotEmpty'))
  1206. {
  1207. $validators = $this->getValidators();
  1208. $notEmpty = array('validator' => 'NotEmpty', 'breakChainOnFailure' => true);
  1209. array_unshift($validators, $notEmpty);
  1210. $this->setValidators($validators);
  1211. }
  1212. // Find the correct translator. Zend_Validate_Abstract::getDefaultTranslator()
  1213. // will get either the static translator attached to Zend_Validate_Abstract
  1214. // or the 'Zend_Translate' from Zend_Registry.
  1215. if (Zend_Validate_Abstract::hasDefaultTranslator() &&
  1216. !Zend_Form::hasDefaultTranslator())
  1217. {
  1218. $translator = Zend_Validate_Abstract::getDefaultTranslator();
  1219. if ($this->hasTranslator()) {
  1220. // only pick up this element's translator if it was attached directly.
  1221. $translator = $this->getTranslator();
  1222. }
  1223. } else {
  1224. $translator = $this->getTranslator();
  1225. }
  1226. $this->_messages = array();
  1227. $this->_errors = array();
  1228. $result = true;
  1229. $isArray = $this->isArray();
  1230. foreach ($this->getValidators() as $key => $validator) {
  1231. if (method_exists($validator, 'setTranslator')) {
  1232. if (method_exists($validator, 'hasTranslator')) {
  1233. if (!$validator->hasTranslator()) {
  1234. $validator->setTranslator($translator);
  1235. }
  1236. } else {
  1237. $validator->setTranslator($translator);
  1238. }
  1239. }
  1240. if (method_exists($validator, 'setDisableTranslator')) {
  1241. $validator->setDisableTranslator($this->translatorIsDisabled());
  1242. }
  1243. if ($isArray && is_array($value)) {
  1244. $messages = array();
  1245. $errors = array();
  1246. if (empty($value)) {
  1247. if ($this->isRequired()
  1248. || (!$this->isRequired() && !$this->getAllowEmpty())
  1249. ) {
  1250. $value = '';
  1251. }
  1252. }
  1253. foreach ((array)$value as $val) {
  1254. if (!$validator->isValid($val, $context)) {
  1255. $result = false;
  1256. if ($this->_hasErrorMessages()) {
  1257. $messages = $this->_getErrorMessages();
  1258. $errors = $messages;
  1259. } else {
  1260. $messages = array_merge($messages, $validator->getMessages());
  1261. $errors = array_merge($errors, $validator->getErrors());
  1262. }
  1263. }
  1264. }
  1265. if ($result) {
  1266. continue;
  1267. }
  1268. } elseif ($validator->isValid($value, $context)) {
  1269. continue;
  1270. } else {
  1271. $result = false;
  1272. if ($this->_hasErrorMessages()) {
  1273. $messages = $this->_getErrorMessages();
  1274. $errors = $messages;
  1275. } else {
  1276. $messages = $validator->getMessages();
  1277. $errors = array_keys($messages);
  1278. }
  1279. }
  1280. $result = false;
  1281. $this->_messages = array_merge($this->_messages, $messages);
  1282. $this->_errors = array_merge($this->_errors, $errors);
  1283. if ($validator->zfBreakChainOnFailure) {
  1284. break;
  1285. }
  1286. }
  1287. // If element manually flagged as invalid, return false
  1288. if ($this->_isErrorForced) {
  1289. return false;
  1290. }
  1291. return $result;
  1292. }
  1293. /**
  1294. * Add a custom error message to return in the event of failed validation
  1295. *
  1296. * @param string $message
  1297. * @return Zend_Form_Element
  1298. */
  1299. public function addErrorMessage($message)
  1300. {
  1301. $this->_errorMessages[] = (string) $message;
  1302. return $this;
  1303. }
  1304. /**
  1305. * Add multiple custom error messages to return in the event of failed validation
  1306. *
  1307. * @param array $messages
  1308. * @return Zend_Form_Element
  1309. */
  1310. public function addErrorMessages(array $messages)
  1311. {
  1312. foreach ($messages as $message) {
  1313. $this->addErrorMessage($message);
  1314. }
  1315. return $this;
  1316. }
  1317. /**
  1318. * Same as addErrorMessages(), but clears custom error message stack first
  1319. *
  1320. * @param array $messages
  1321. * @return Zend_Form_Element
  1322. */
  1323. public function setErrorMessages(array $messages)
  1324. {
  1325. $this->clearErrorMessages();
  1326. return $this->addErrorMessages($messages);
  1327. }
  1328. /**
  1329. * Retrieve custom error messages
  1330. *
  1331. * @return array
  1332. */
  1333. public function getErrorMessages()
  1334. {
  1335. return $this->_errorMessages;
  1336. }
  1337. /**
  1338. * Clear custom error messages stack
  1339. *
  1340. * @return Zend_Form_Element
  1341. */
  1342. public function clearErrorMessages()
  1343. {
  1344. $this->_errorMessages = array();
  1345. return $this;
  1346. }
  1347. /**
  1348. * Get errorMessageSeparator
  1349. *
  1350. * @return string
  1351. */
  1352. public function getErrorMessageSeparator()
  1353. {
  1354. return $this->_errorMessageSeparator;
  1355. }
  1356. /**
  1357. * Set errorMessageSeparator
  1358. *
  1359. * @param string $separator
  1360. * @return Zend_Form_Element
  1361. */
  1362. public function setErrorMessageSeparator($separator)
  1363. {
  1364. $this->_errorMessageSeparator = $separator;
  1365. return $this;
  1366. }
  1367. /**
  1368. * Mark the element as being in a failed validation state
  1369. *
  1370. * @return Zend_Form_Element
  1371. */
  1372. public function markAsError()
  1373. {
  1374. $messages = $this->getMessages();
  1375. $customMessages = $this->_getErrorMessages();
  1376. $messages = $messages + $customMessages;
  1377. if (empty($messages)) {
  1378. $this->_isError = true;
  1379. } else {
  1380. $this->_messages = $messages;
  1381. }
  1382. $this->_isErrorForced = true;
  1383. return $this;
  1384. }
  1385. /**
  1386. * Add an error message and mark element as failed validation
  1387. *
  1388. * @param string $message
  1389. * @return Zend_Form_Element
  1390. */
  1391. public function addError($message)
  1392. {
  1393. $this->addErrorMessage($message);
  1394. $this->markAsError();
  1395. return $this;
  1396. }
  1397. /**
  1398. * Add multiple error messages and flag element as failed validation
  1399. *
  1400. * @param array $messages
  1401. * @return Zend_Form_Element
  1402. */
  1403. public function addErrors(array $messages)
  1404. {
  1405. foreach ($messages as $message) {
  1406. $this->addError($message);
  1407. }
  1408. return $this;
  1409. }
  1410. /**
  1411. * Overwrite any previously set error messages and flag as failed validation
  1412. *
  1413. * @param array $messages
  1414. * @return Zend_Form_Element
  1415. */
  1416. public function setErrors(array $messages)
  1417. {
  1418. $this->clearErrorMessages();
  1419. return $this->addErrors($messages);
  1420. }
  1421. /**
  1422. * Are there errors registered?
  1423. *
  1424. * @return bool
  1425. */
  1426. public function hasErrors()
  1427. {
  1428. return (!empty($this->_messages) || $this->_isError);
  1429. }
  1430. /**
  1431. * Retrieve validator chain errors
  1432. *
  1433. * @return array
  1434. */
  1435. public function getErrors()
  1436. {
  1437. return $this->_errors;
  1438. }
  1439. /**
  1440. * Retrieve error messages
  1441. *
  1442. * @return array
  1443. */
  1444. public function getMessages()
  1445. {
  1446. return $this->_messages;
  1447. }
  1448. // Filtering
  1449. /**
  1450. * Add a filter to the element
  1451. *
  1452. * @param string|Zend_Filter_Interface $filter
  1453. * @return Zend_Form_Element
  1454. */
  1455. public function addFilter($filter, $options = array())
  1456. {
  1457. if ($filter instanceof Zend_Filter_Interface) {
  1458. $name = get_class($filter);
  1459. } elseif (is_string($filter)) {
  1460. $name = $filter;
  1461. $filter = array(
  1462. 'filter' => $filter,
  1463. 'options' => $options,
  1464. );
  1465. $this->_filters[$name] = $filter;
  1466. } else {
  1467. require_once 'Zend/Form/Exception.php';
  1468. throw new Zend_Form_Exception('Invalid filter provided to addFilter; must be string or Zend_Filter_Interface');
  1469. }
  1470. $this->_filters[$name] = $filter;
  1471. return $this;
  1472. }
  1473. /**
  1474. * Add filters to element
  1475. *
  1476. * @param array $filters
  1477. * @return Zend_Form_Element
  1478. */
  1479. public function addFilters(array $filters)
  1480. {
  1481. foreach ($filters as $filterInfo) {
  1482. if (is_string($filterInfo)) {
  1483. $this->addFilter($filterInfo);
  1484. } elseif ($filterInfo instanceof Zend_Filter_Interface) {
  1485. $this->addFilter($filterInfo);
  1486. } elseif (is_array($filterInfo)) {
  1487. $argc = count($filterInfo);
  1488. $options = array();
  1489. if (isset($filterInfo['filter'])) {
  1490. $filter = $filterInfo['filter'];
  1491. if (isset($filterInfo['options'])) {
  1492. $options = $filterInfo['options'];
  1493. }
  1494. $this->addFilter($filter, $options);
  1495. } else {
  1496. switch (true) {
  1497. case (0 == $argc):
  1498. break;
  1499. case (1 <= $argc):
  1500. $filter = array_shift($filterInfo);
  1501. case (2 <= $argc):
  1502. $options = array_shift($filterInfo);
  1503. default:
  1504. $this->addFilter($filter, $options);
  1505. break;
  1506. }
  1507. }
  1508. } else {
  1509. require_once 'Zend/Form/Exception.php';
  1510. throw new Zend_Form_Exception('Invalid filter passed to addFilters()');
  1511. }
  1512. }
  1513. return $this;
  1514. }
  1515. /**
  1516. * Add filters to element, overwriting any already existing
  1517. *
  1518. * @param array $filters
  1519. * @return Zend_Form_Element
  1520. */
  1521. public function setFilters(array $filters)
  1522. {
  1523. $this->clearFilters();
  1524. return $this->addFilters($filters);
  1525. }
  1526. /**
  1527. * Retrieve a single filter by name
  1528. *
  1529. * @param string $name
  1530. * @return Zend_Filter_Interface
  1531. */
  1532. public function getFilter($name)
  1533. {
  1534. if (!isset($this->_filters[$name])) {
  1535. $len = strlen($name);
  1536. foreach ($this->_filters as $localName => $filter) {
  1537. if ($len > strlen($localName)) {
  1538. continue;
  1539. }
  1540. if (0 === substr_compare($localName, $name, -$len, $len, true)) {
  1541. if (is_array($filter)) {
  1542. return $this->_loadFilter($filter);
  1543. }
  1544. return $filter;
  1545. }
  1546. }
  1547. return false;
  1548. }
  1549. if (is_array($this->_filters[$name])) {
  1550. return $this->_loadFilter($this->_filters[$name]);
  1551. }
  1552. return $this->_filters[$name];
  1553. }
  1554. /**
  1555. * Get all filters
  1556. *
  1557. * @return array
  1558. */
  1559. public function getFilters()
  1560. {
  1561. $filters = array();
  1562. foreach ($this->_filters as $key => $value) {
  1563. if ($value instanceof Zend_Filter_Interface) {
  1564. $filters[$key] = $value;
  1565. continue;
  1566. }
  1567. $filter = $this->_loadFilter($value);
  1568. $filters[get_class($filter)] = $filter;
  1569. }
  1570. return $filters;
  1571. }
  1572. /**
  1573. * Remove a filter by name
  1574. *
  1575. * @param string $name
  1576. * @return Zend_Form_Element
  1577. */
  1578. public function removeFilter($name)
  1579. {
  1580. if (isset($this->_filters[$name])) {
  1581. unset($this->_filters[$name]);
  1582. } else {
  1583. $len = strlen($name);
  1584. foreach (array_keys($this->_filters) as $filter) {
  1585. if ($len > strlen($filter)) {
  1586. continue;
  1587. }
  1588. if (0 === substr_compare($filter, $name, -$len, $len, true)) {
  1589. unset($this->_filters[$filter]);
  1590. break;
  1591. }
  1592. }
  1593. }
  1594. return $this;
  1595. }
  1596. /**
  1597. * Clear all filters
  1598. *
  1599. * @return Zend_Form_Element
  1600. */
  1601. public function clearFilters()
  1602. {
  1603. $this->_filters = array();
  1604. return $this;
  1605. }
  1606. // Rendering
  1607. /**
  1608. * Set view object
  1609. *
  1610. * @param Zend_View_Interface $view
  1611. * @return Zend_Form_Element
  1612. */
  1613. public function setView(Zend_View_Interface $view = null)
  1614. {
  1615. $this->_view = $view;
  1616. return $this;
  1617. }
  1618. /**
  1619. * Retrieve view object
  1620. *
  1621. * Retrieves from ViewRenderer if none previously set.
  1622. *
  1623. * @return null|Zend_View_Interface
  1624. */
  1625. public function getView()
  1626. {
  1627. if (null === $this->_view) {
  1628. require_once 'Zend/Controller/Action/HelperBroker.php';
  1629. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  1630. $this->setView($viewRenderer->view);
  1631. }
  1632. return $this->_view;
  1633. }
  1634. /**
  1635. * Instantiate a decorator based on class name or class name fragment
  1636. *
  1637. * @param string $name
  1638. * @param null|array $options
  1639. * @return Zend_Form_Decorator_Interface
  1640. */
  1641. protected function _getDecorator($name, $options)
  1642. {
  1643. $class = $this->getPluginLoader(self::DECORATOR)->load($name);
  1644. if (null === $options) {
  1645. $decorator = new $class;
  1646. } else {
  1647. $decorator = new $class($options);
  1648. }
  1649. return $decorator;
  1650. }
  1651. /**
  1652. * Add a decorator for rendering the element
  1653. *
  1654. * @param string|Zend_Form_Decorator_Interface $decorator
  1655. * @param array|Zend_Config $options Options with which to initialize decorator
  1656. * @return Zend_Form_Element
  1657. */
  1658. public function addDecorator($decorator, $options = null)
  1659. {
  1660. if ($decorator instanceof Zend_Form_Decorator_Interface) {
  1661. $name = get_class($decorator);
  1662. } elseif (is_string($decorator)) {
  1663. $name = $decorator;
  1664. $decorator = array(
  1665. 'decorator' => $name,
  1666. 'options' => $options,
  1667. );
  1668. } elseif (is_array($decorator)) {
  1669. foreach ($decorator as $name => $spec) {
  1670. break;
  1671. }
  1672. if (is_numeric($name)) {
  1673. require_once 'Zend/Form/Exception.php';
  1674. throw new Zend_Form_Exception('Invalid alias provided to addDecorator; must be alphanumeric string');
  1675. }
  1676. if (is_string($spec))

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