/protected/vendors/Zend/Form.php
PHP | 3401 lines | 1972 code | 339 blank | 1090 comment | 321 complexity | 856e4f984bb5b3413759fa042ba148f7 MD5 | raw file
Possible License(s): MIT, LGPL-2.1, BSD-3-Clause, Apache-2.0
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Form
- * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- /** @see Zend_Validate_Interface */
- require_once 'Zend/Validate/Interface.php';
- /**
- * Zend_Form
- *
- * @category Zend
- * @package Zend_Form
- * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id: Form.php 23950 2011-05-03 03:46:42Z ralph $
- */
- class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
- {
- /**#@+
- * Plugin loader type constants
- */
- const DECORATOR = 'DECORATOR';
- const ELEMENT = 'ELEMENT';
- /**#@-*/
- /**#@+
- * Method type constants
- */
- const METHOD_DELETE = 'delete';
- const METHOD_GET = 'get';
- const METHOD_POST = 'post';
- const METHOD_PUT = 'put';
- /**#@-*/
- /**#@+
- * Encoding type constants
- */
- const ENCTYPE_URLENCODED = 'application/x-www-form-urlencoded';
- const ENCTYPE_MULTIPART = 'multipart/form-data';
- /**#@-*/
- /**
- * Form metadata and attributes
- * @var array
- */
- protected $_attribs = array();
- /**
- * Decorators for rendering
- * @var array
- */
- protected $_decorators = array();
- /**
- * Default display group class
- * @var string
- */
- protected $_defaultDisplayGroupClass = 'Zend_Form_DisplayGroup';
- /**
- * Form description
- * @var string
- */
- protected $_description;
- /**
- * Should we disable loading the default decorators?
- * @var bool
- */
- protected $_disableLoadDefaultDecorators = false;
- /**
- * Display group prefix paths
- * @var array
- */
- protected $_displayGroupPrefixPaths = array();
- /**
- * Groups of elements grouped for display purposes
- * @var array
- */
- protected $_displayGroups = array();
- /**
- * Global decorators to apply to all elements
- * @var null|array
- */
- protected $_elementDecorators;
- /**
- * Prefix paths to use when creating elements
- * @var array
- */
- protected $_elementPrefixPaths = array();
- /**
- * Form elements
- * @var array
- */
- protected $_elements = array();
- /**
- * Array to which elements belong (if any)
- * @var string
- */
- protected $_elementsBelongTo;
- /**
- * Custom form-level error messages
- * @var array
- */
- protected $_errorMessages = array();
- /**
- * Are there errors in the form?
- * @var bool
- */
- protected $_errorsExist = false;
- /**
- * Has the form been manually flagged as an error?
- * @var bool
- */
- protected $_errorsForced = false;
- /**
- * Form order
- * @var int|null
- */
- protected $_formOrder;
- /**
- * Whether or not form elements are members of an array
- * @var bool
- */
- protected $_isArray = false;
- /**
- * Form legend
- * @var string
- */
- protected $_legend;
- /**
- * Plugin loaders
- * @var array
- */
- protected $_loaders = array();
- /**
- * Allowed form methods
- * @var array
- */
- protected $_methods = array('delete', 'get', 'post', 'put');
- /**
- * Order in which to display and iterate elements
- * @var array
- */
- protected $_order = array();
- /**
- * Whether internal order has been updated or not
- * @var bool
- */
- protected $_orderUpdated = false;
- /**
- * Sub form prefix paths
- * @var array
- */
- protected $_subFormPrefixPaths = array();
- /**
- * Sub forms
- * @var array
- */
- protected $_subForms = array();
- /**
- * @var Zend_Translate
- */
- protected $_translator;
- /**
- * Global default translation adapter
- * @var Zend_Translate
- */
- protected static $_translatorDefault;
- /**
- * is the translator disabled?
- * @var bool
- */
- protected $_translatorDisabled = false;
- /**
- * @var Zend_View_Interface
- */
- protected $_view;
- /**
- * @var bool
- */
- protected $_isRendered = false;
- /**
- * Constructor
- *
- * Registers form view helper as decorator
- *
- * @param mixed $options
- * @return void
- */
- public function __construct($options = null)
- {
- if (is_array($options)) {
- $this->setOptions($options);
- } elseif ($options instanceof Zend_Config) {
- $this->setConfig($options);
- }
- // Extensions...
- $this->init();
- $this->loadDefaultDecorators();
- }
- /**
- * Clone form object and all children
- *
- * @return void
- */
- public function __clone()
- {
- $elements = array();
- foreach ($this->getElements() as $name => $element) {
- $elements[] = clone $element;
- }
- $this->setElements($elements);
- $subForms = array();
- foreach ($this->getSubForms() as $name => $subForm) {
- $subForms[$name] = clone $subForm;
- }
- $this->setSubForms($subForms);
- $displayGroups = array();
- foreach ($this->_displayGroups as $group) {
- $clone = clone $group;
- $elements = array();
- foreach ($clone->getElements() as $name => $e) {
- $elements[] = $this->getElement($name);
- }
- $clone->setElements($elements);
- $displayGroups[] = $clone;
- }
- $this->setDisplayGroups($displayGroups);
- }
- /**
- * Reset values of form
- *
- * @return Zend_Form
- */
- public function reset()
- {
- foreach ($this->getElements() as $element) {
- $element->setValue(null);
- }
- foreach ($this->getSubForms() as $subForm) {
- $subForm->reset();
- }
- return $this;
- }
- /**
- * Initialize form (used by extending classes)
- *
- * @return void
- */
- public function init()
- {
- }
- /**
- * Set form state from options array
- *
- * @param array $options
- * @return Zend_Form
- */
- public function setOptions(array $options)
- {
- if (isset($options['prefixPath'])) {
- $this->addPrefixPaths($options['prefixPath']);
- unset($options['prefixPath']);
- }
- if (isset($options['elementPrefixPath'])) {
- $this->addElementPrefixPaths($options['elementPrefixPath']);
- unset($options['elementPrefixPath']);
- }
- if (isset($options['displayGroupPrefixPath'])) {
- $this->addDisplayGroupPrefixPaths($options['displayGroupPrefixPath']);
- unset($options['displayGroupPrefixPath']);
- }
- if (isset($options['elementDecorators'])) {
- $this->_elementDecorators = $options['elementDecorators'];
- unset($options['elementDecorators']);
- }
- if (isset($options['elements'])) {
- $this->setElements($options['elements']);
- unset($options['elements']);
- }
- if (isset($options['defaultDisplayGroupClass'])) {
- $this->setDefaultDisplayGroupClass($options['defaultDisplayGroupClass']);
- unset($options['defaultDisplayGroupClass']);
- }
- if (isset($options['displayGroupDecorators'])) {
- $displayGroupDecorators = $options['displayGroupDecorators'];
- unset($options['displayGroupDecorators']);
- }
- if (isset($options['elementsBelongTo'])) {
- $elementsBelongTo = $options['elementsBelongTo'];
- unset($options['elementsBelongTo']);
- }
- if (isset($options['attribs'])) {
- $this->addAttribs($options['attribs']);
- unset($options['attribs']);
- }
- $forbidden = array(
- 'Options', 'Config', 'PluginLoader', 'SubForms', 'Translator',
- 'Attrib', 'Default',
- );
- foreach ($options as $key => $value) {
- $normalized = ucfirst($key);
- if (in_array($normalized, $forbidden)) {
- continue;
- }
- $method = 'set' . $normalized;
- if (method_exists($this, $method)) {
- if($normalized == 'View' && !($value instanceof Zend_View_Interface)) {
- continue;
- }
- $this->$method($value);
- } else {
- $this->setAttrib($key, $value);
- }
- }
- if (isset($displayGroupDecorators)) {
- $this->setDisplayGroupDecorators($displayGroupDecorators);
- }
- if (isset($elementsBelongTo)) {
- $this->setElementsBelongTo($elementsBelongTo);
- }
- return $this;
- }
- /**
- * Set form state from config object
- *
- * @param Zend_Config $config
- * @return Zend_Form
- */
- public function setConfig(Zend_Config $config)
- {
- return $this->setOptions($config->toArray());
- }
- // Loaders
- /**
- * Set plugin loaders for use with decorators and elements
- *
- * @param Zend_Loader_PluginLoader_Interface $loader
- * @param string $type 'decorator' or 'element'
- * @return Zend_Form
- * @throws Zend_Form_Exception on invalid type
- */
- public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type = null)
- {
- $type = strtoupper($type);
- switch ($type) {
- case self::DECORATOR:
- case self::ELEMENT:
- $this->_loaders[$type] = $loader;
- return $this;
- default:
- require_once 'Zend/Form/Exception.php';
- throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type));
- }
- }
- /**
- * Retrieve plugin loader for given type
- *
- * $type may be one of:
- * - decorator
- * - element
- *
- * If a plugin loader does not exist for the given type, defaults are
- * created.
- *
- * @param string $type
- * @return Zend_Loader_PluginLoader_Interface
- */
- public function getPluginLoader($type = null)
- {
- $type = strtoupper($type);
- if (!isset($this->_loaders[$type])) {
- switch ($type) {
- case self::DECORATOR:
- $prefixSegment = 'Form_Decorator';
- $pathSegment = 'Form/Decorator';
- break;
- case self::ELEMENT:
- $prefixSegment = 'Form_Element';
- $pathSegment = 'Form/Element';
- break;
- default:
- require_once 'Zend/Form/Exception.php';
- throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
- }
- require_once 'Zend/Loader/PluginLoader.php';
- $this->_loaders[$type] = new Zend_Loader_PluginLoader(
- array('Zend_' . $prefixSegment . '_' => 'Zend/' . $pathSegment . '/')
- );
- }
- return $this->_loaders[$type];
- }
- /**
- * Add prefix path for plugin loader
- *
- * If no $type specified, assumes it is a base path for both filters and
- * validators, and sets each according to the following rules:
- * - decorators: $prefix = $prefix . '_Decorator'
- * - elements: $prefix = $prefix . '_Element'
- *
- * Otherwise, the path prefix is set on the appropriate plugin loader.
- *
- * If $type is 'decorator', sets the path in the decorator plugin loader
- * for all elements. Additionally, if no $type is provided,
- * the prefix and path is added to both decorator and element
- * plugin loader with following settings:
- * $prefix . '_Decorator', $path . '/Decorator/'
- * $prefix . '_Element', $path . '/Element/'
- *
- * @param string $prefix
- * @param string $path
- * @param string $type
- * @return Zend_Form
- * @throws Zend_Form_Exception for invalid type
- */
- public function addPrefixPath($prefix, $path, $type = null)
- {
- $type = strtoupper($type);
- switch ($type) {
- case self::DECORATOR:
- case self::ELEMENT:
- $loader = $this->getPluginLoader($type);
- $loader->addPrefixPath($prefix, $path);
- return $this;
- case null:
- $prefix = rtrim($prefix, '_');
- $path = rtrim($path, DIRECTORY_SEPARATOR);
- foreach (array(self::DECORATOR, self::ELEMENT) as $type) {
- $cType = ucfirst(strtolower($type));
- $pluginPath = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR;
- $pluginPrefix = $prefix . '_' . $cType;
- $loader = $this->getPluginLoader($type);
- $loader->addPrefixPath($pluginPrefix, $pluginPath);
- }
- return $this;
- default:
- require_once 'Zend/Form/Exception.php';
- throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
- }
- }
- /**
- * Add many prefix paths at once
- *
- * @param array $spec
- * @return Zend_Form
- */
- public function addPrefixPaths(array $spec)
- {
- if (isset($spec['prefix']) && isset($spec['path'])) {
- return $this->addPrefixPath($spec['prefix'], $spec['path']);
- }
- foreach ($spec as $type => $paths) {
- if (is_numeric($type) && is_array($paths)) {
- $type = null;
- if (isset($paths['prefix']) && isset($paths['path'])) {
- if (isset($paths['type'])) {
- $type = $paths['type'];
- }
- $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
- }
- } elseif (!is_numeric($type)) {
- if (!isset($paths['prefix']) || !isset($paths['path'])) {
- continue;
- }
- $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
- }
- }
- return $this;
- }
- /**
- * Add prefix path for all elements
- *
- * @param string $prefix
- * @param string $path
- * @param string $type
- * @return Zend_Form
- */
- public function addElementPrefixPath($prefix, $path, $type = null)
- {
- $this->_elementPrefixPaths[] = array(
- 'prefix' => $prefix,
- 'path' => $path,
- 'type' => $type,
- );
- foreach ($this->getElements() as $element) {
- $element->addPrefixPath($prefix, $path, $type);
- }
- foreach ($this->getSubForms() as $subForm) {
- $subForm->addElementPrefixPath($prefix, $path, $type);
- }
- return $this;
- }
- /**
- * Add prefix paths for all elements
- *
- * @param array $spec
- * @return Zend_Form
- */
- public function addElementPrefixPaths(array $spec)
- {
- $this->_elementPrefixPaths = $this->_elementPrefixPaths + $spec;
- foreach ($this->getElements() as $element) {
- $element->addPrefixPaths($spec);
- }
- return $this;
- }
- /**
- * Add prefix path for all display groups
- *
- * @param string $prefix
- * @param string $path
- * @return Zend_Form
- */
- public function addDisplayGroupPrefixPath($prefix, $path)
- {
- $this->_displayGroupPrefixPaths[] = array(
- 'prefix' => $prefix,
- 'path' => $path,
- );
- foreach ($this->getDisplayGroups() as $group) {
- $group->addPrefixPath($prefix, $path);
- }
- return $this;
- }
- /**
- * Add multiple display group prefix paths at once
- *
- * @param array $spec
- * @return Zend_Form
- */
- public function addDisplayGroupPrefixPaths(array $spec)
- {
- foreach ($spec as $key => $value) {
- if (is_string($value) && !is_numeric($key)) {
- $this->addDisplayGroupPrefixPath($key, $value);
- continue;
- }
- if (is_string($value) && is_numeric($key)) {
- continue;
- }
- if (is_array($value)) {
- $count = count($value);
- if (array_keys($value) === range(0, $count - 1)) {
- if ($count < 2) {
- continue;
- }
- $prefix = array_shift($value);
- $path = array_shift($value);
- $this->addDisplayGroupPrefixPath($prefix, $path);
- continue;
- }
- if (array_key_exists('prefix', $value) && array_key_exists('path', $value)) {
- $this->addDisplayGroupPrefixPath($value['prefix'], $value['path']);
- }
- }
- }
- return $this;
- }
- // Form metadata:
- /**
- * Set form attribute
- *
- * @param string $key
- * @param mixed $value
- * @return Zend_Form
- */
- public function setAttrib($key, $value)
- {
- $key = (string) $key;
- $this->_attribs[$key] = $value;
- return $this;
- }
- /**
- * Add multiple form attributes at once
- *
- * @param array $attribs
- * @return Zend_Form
- */
- public function addAttribs(array $attribs)
- {
- foreach ($attribs as $key => $value) {
- $this->setAttrib($key, $value);
- }
- return $this;
- }
- /**
- * Set multiple form attributes at once
- *
- * Overwrites any previously set attributes.
- *
- * @param array $attribs
- * @return Zend_Form
- */
- public function setAttribs(array $attribs)
- {
- $this->clearAttribs();
- return $this->addAttribs($attribs);
- }
- /**
- * Retrieve a single form attribute
- *
- * @param string $key
- * @return mixed
- */
- public function getAttrib($key)
- {
- $key = (string) $key;
- if (!isset($this->_attribs[$key])) {
- return null;
- }
- return $this->_attribs[$key];
- }
- /**
- * Retrieve all form attributes/metadata
- *
- * @return array
- */
- public function getAttribs()
- {
- return $this->_attribs;
- }
- /**
- * Remove attribute
- *
- * @param string $key
- * @return bool
- */
- public function removeAttrib($key)
- {
- if (isset($this->_attribs[$key])) {
- unset($this->_attribs[$key]);
- return true;
- }
- return false;
- }
- /**
- * Clear all form attributes
- *
- * @return Zend_Form
- */
- public function clearAttribs()
- {
- $this->_attribs = array();
- return $this;
- }
- /**
- * Set form action
- *
- * @param string $action
- * @return Zend_Form
- */
- public function setAction($action)
- {
- return $this->setAttrib('action', (string) $action);
- }
- /**
- * Get form action
- *
- * Sets default to '' if not set.
- *
- * @return string
- */
- public function getAction()
- {
- $action = $this->getAttrib('action');
- if (null === $action) {
- $action = '';
- $this->setAction($action);
- }
- return $action;
- }
- /**
- * Set form method
- *
- * Only values in {@link $_methods()} allowed
- *
- * @param string $method
- * @return Zend_Form
- * @throws Zend_Form_Exception
- */
- public function setMethod($method)
- {
- $method = strtolower($method);
- if (!in_array($method, $this->_methods)) {
- require_once 'Zend/Form/Exception.php';
- throw new Zend_Form_Exception(sprintf('"%s" is an invalid form method', $method));
- }
- $this->setAttrib('method', $method);
- return $this;
- }
- /**
- * Retrieve form method
- *
- * @return string
- */
- public function getMethod()
- {
- if (null === ($method = $this->getAttrib('method'))) {
- $method = self::METHOD_POST;
- $this->setAttrib('method', $method);
- }
- return strtolower($method);
- }
- /**
- * Set encoding type
- *
- * @param string $value
- * @return Zend_Form
- */
- public function setEnctype($value)
- {
- $this->setAttrib('enctype', $value);
- return $this;
- }
- /**
- * Get encoding type
- *
- * @return string
- */
- public function getEnctype()
- {
- if (null === ($enctype = $this->getAttrib('enctype'))) {
- $enctype = self::ENCTYPE_URLENCODED;
- $this->setAttrib('enctype', $enctype);
- }
- return $this->getAttrib('enctype');
- }
- /**
- * Filter a name to only allow valid variable characters
- *
- * @param string $value
- * @param bool $allowBrackets
- * @return string
- */
- public function filterName($value, $allowBrackets = false)
- {
- $charset = '^a-zA-Z0-9_\x7f-\xff';
- if ($allowBrackets) {
- $charset .= '\[\]';
- }
- return preg_replace('/[' . $charset . ']/', '', (string) $value);
- }
- /**
- * Set form name
- *
- * @param string $name
- * @return Zend_Form
- */
- public function setName($name)
- {
- $name = $this->filterName($name);
- if ('' === (string)$name) {
- require_once 'Zend/Form/Exception.php';
- throw new Zend_Form_Exception('Invalid name provided; must contain only valid variable characters and be non-empty');
- }
- return $this->setAttrib('name', $name);
- }
- /**
- * Get name attribute
- *
- * @return null|string
- */
- public function getName()
- {
- return $this->getAttrib('name');
- }
- /**
- * Get fully qualified name
- *
- * Places name as subitem of array and/or appends brackets.
- *
- * @return string
- */
- public function getFullyQualifiedName()
- {
- return $this->getName();
- }
- /**
- * Get element id
- *
- * @return string
- */
- public function getId()
- {
- if (null !== ($id = $this->getAttrib('id'))) {
- return $id;
- }
- $id = $this->getFullyQualifiedName();
- // Bail early if no array notation detected
- if (!strstr($id, '[')) {
- return $id;
- }
- // Strip array notation
- if ('[]' == substr($id, -2)) {
- $id = substr($id, 0, strlen($id) - 2);
- }
- $id = str_replace('][', '-', $id);
- $id = str_replace(array(']', '['), '-', $id);
- $id = trim($id, '-');
- return $id;
- }
- /**
- * Set form legend
- *
- * @param string $value
- * @return Zend_Form
- */
- public function setLegend($value)
- {
- $this->_legend = (string) $value;
- return $this;
- }
- /**
- * Get form legend
- *
- * @return string
- */
- public function getLegend()
- {
- return $this->_legend;
- }
- /**
- * Set form description
- *
- * @param string $value
- * @return Zend_Form
- */
- public function setDescription($value)
- {
- $this->_description = (string) $value;
- return $this;
- }
- /**
- * Retrieve form description
- *
- * @return string
- */
- public function getDescription()
- {
- return $this->_description;
- }
- /**
- * Set form order
- *
- * @param int $index
- * @return Zend_Form
- */
- public function setOrder($index)
- {
- $this->_formOrder = (int) $index;
- return $this;
- }
- /**
- * Get form order
- *
- * @return int|null
- */
- public function getOrder()
- {
- return $this->_formOrder;
- }
- /**
- * When calling renderFormElements or render this method
- * is used to set $_isRendered member to prevent repeatedly
- * merging belongsTo setting
- */
- protected function _setIsRendered()
- {
- $this->_isRendered = true;
- return $this;
- }
- /**
- * Get the value of $_isRendered member
- */
- protected function _getIsRendered()
- {
- return (bool)$this->_isRendered;
- }
- // Element interaction:
- /**
- * Add a new element
- *
- * $element may be either a string element type, or an object of type
- * Zend_Form_Element. If a string element type is provided, $name must be
- * provided, and $options may be optionally provided for configuring the
- * element.
- *
- * If a Zend_Form_Element is provided, $name may be optionally provided,
- * and any provided $options will be ignored.
- *
- * @param string|Zend_Form_Element $element
- * @param string $name
- * @param array|Zend_Config $options
- * @throws Zend_Form_Exception on invalid element
- * @return Zend_Form
- */
- public function addElement($element, $name = null, $options = null)
- {
- if (is_string($element)) {
- if (null === $name) {
- require_once 'Zend/Form/Exception.php';
- throw new Zend_Form_Exception('Elements specified by string must have an accompanying name');
- }
- if (is_array($this->_elementDecorators)) {
- if (null === $options) {
- $options = array('decorators' => $this->_elementDecorators);
- } elseif ($options instanceof Zend_Config) {
- $options = $options->toArray();
- }
- if (is_array($options)
- && !array_key_exists('decorators', $options)
- ) {
- $options['decorators'] = $this->_elementDecorators;
- }
- }
- $this->_elements[$name] = $this->createElement($element, $name, $options);
- } elseif ($element instanceof Zend_Form_Element) {
- $prefixPaths = array();
- $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
- if (!empty($this->_elementPrefixPaths)) {
- $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
- }
- if (null === $name) {
- $name = $element->getName();
- }
- $this->_elements[$name] = $element;
- $this->_elements[$name]->addPrefixPaths($prefixPaths);
- } else {
- require_once 'Zend/Form/Exception.php';
- throw new Zend_Form_Exception('Element must be specified by string or Zend_Form_Element instance');
- }
- $this->_order[$name] = $this->_elements[$name]->getOrder();
- $this->_orderUpdated = true;
- $this->_setElementsBelongTo($name);
- return $this;
- }
- /**
- * Create an element
- *
- * Acts as a factory for creating elements. Elements created with this
- * method will not be attached to the form, but will contain element
- * settings as specified in the form object (including plugin loader
- * prefix paths, default decorators, etc.).
- *
- * @param string $type
- * @param string $name
- * @param array|Zend_Config $options
- * @return Zend_Form_Element
- */
- public function createElement($type, $name, $options = null)
- {
- if (!is_string($type)) {
- require_once 'Zend/Form/Exception.php';
- throw new Zend_Form_Exception('Element type must be a string indicating type');
- }
- if (!is_string($name)) {
- require_once 'Zend/Form/Exception.php';
- throw new Zend_Form_Exception('Element name must be a string');
- }
- $prefixPaths = array();
- $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
- if (!empty($this->_elementPrefixPaths)) {
- $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
- }
- if ($options instanceof Zend_Config) {
- $options = $options->toArray();
- }
- if ((null === $options) || !is_array($options)) {
- $options = array('prefixPath' => $prefixPaths);
- } elseif (is_array($options)) {
- if (array_key_exists('prefixPath', $options)) {
- $options['prefixPath'] = array_merge($prefixPaths, $options['prefixPath']);
- } else {
- $options['prefixPath'] = $prefixPaths;
- }
- }
- $class = $this->getPluginLoader(self::ELEMENT)->load($type);
- $element = new $class($name, $options);
- return $element;
- }
- /**
- * Add multiple elements at once
- *
- * @param array $elements
- * @return Zend_Form
- */
- public function addElements(array $elements)
- {
- foreach ($elements as $key => $spec) {
- $name = null;
- if (!is_numeric($key)) {
- $name = $key;
- }
- if (is_string($spec) || ($spec instanceof Zend_Form_Element)) {
- $this->addElement($spec, $name);
- continue;
- }
- if (is_array($spec)) {
- $argc = count($spec);
- $options = array();
- if (isset($spec['type'])) {
- $type = $spec['type'];
- if (isset($spec['name'])) {
- $name = $spec['name'];
- }
- if (isset($spec['options'])) {
- $options = $spec['options'];
- }
- $this->addElement($type, $name, $options);
- } else {
- switch ($argc) {
- case 0:
- continue;
- case (1 <= $argc):
- $type = array_shift($spec);
- case (2 <= $argc):
- if (null === $name) {
- $name = array_shift($spec);
- } else {
- $options = array_shift($spec);
- }
- case (3 <= $argc):
- if (empty($options)) {
- $options = array_shift($spec);
- }
- default:
- $this->addElement($type, $name, $options);
- }
- }
- }
- }
- return $this;
- }
- /**
- * Set form elements (overwrites existing elements)
- *
- * @param array $elements
- * @return Zend_Form
- */
- public function setElements(array $elements)
- {
- $this->clearElements();
- return $this->addElements($elements);
- }
- /**
- * Retrieve a single element
- *
- * @param string $name
- * @return Zend_Form_Element|null
- */
- public function getElement($name)
- {
- if (array_key_exists($name, $this->_elements)) {
- return $this->_elements[$name];
- }
- return null;
- }
- /**
- * Retrieve all elements
- *
- * @return array
- */
- public function getElements()
- {
- return $this->_elements;
- }
- /**
- * Remove element
- *
- * @param string $name
- * @return boolean
- */
- public function removeElement($name)
- {
- $name = (string) $name;
- if (isset($this->_elements[$name])) {
- unset($this->_elements[$name]);
- if (array_key_exists($name, $this->_order)) {
- unset($this->_order[$name]);
- $this->_orderUpdated = true;
- } else {
- foreach ($this->_displayGroups as $group) {
- if (null !== $group->getElement($name)) {
- $group->removeElement($name);
- }
- }
- }
- return true;
- }
- return false;
- }
- /**
- * Remove all form elements
- *
- * @return Zend_Form
- */
- public function clearElements()
- {
- foreach (array_keys($this->_elements) as $key) {
- if (array_key_exists($key, $this->_order)) {
- unset($this->_order[$key]);
- }
- }
- $this->_elements = array();
- $this->_orderUpdated = true;
- return $this;
- }
- /**
- * Set default values for elements
- *
- * Sets values for all elements specified in the array of $defaults.
- *
- * @param array $defaults
- * @return Zend_Form
- */
- public function setDefaults(array $defaults)
- {
- $eBelongTo = null;
- if ($this->isArray()) {
- $eBelongTo = $this->getElementsBelongTo();
- $defaults = $this->_dissolveArrayValue($defaults, $eBelongTo);
- }
- foreach ($this->getElements() as $name => $element) {
- $check = $defaults;
- if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
- $check = $this->_dissolveArrayValue($defaults, $belongsTo);
- }
- if (array_key_exists($name, $check)) {
- $this->setDefault($name, $check[$name]);
- $defaults = $this->_dissolveArrayUnsetKey($defaults, $belongsTo, $name);
- }
- }
- foreach ($this->getSubForms() as $name => $form) {
- if (!$form->isArray() && array_key_exists($name, $defaults)) {
- $form->setDefaults($defaults[$name]);
- } else {
- $form->setDefaults($defaults);
- }
- }
- return $this;
- }
- /**
- * Set default value for an element
- *
- * @param string $name
- * @param mixed $value
- * @return Zend_Form
- */
- public function setDefault($name, $value)
- {
- $name = (string) $name;
- if ($element = $this->getElement($name)) {
- $element->setValue($value);
- } else {
- if (is_scalar($value)) {
- foreach ($this->getSubForms() as $subForm) {
- $subForm->setDefault($name, $value);
- }
- } elseif (is_array($value) && ($subForm = $this->getSubForm($name))) {
- $subForm->setDefaults($value);
- }
- }
- return $this;
- }
- /**
- * Retrieve value for single element
- *
- * @param string $name
- * @return mixed
- */
- public function getValue($name)
- {
- if ($element = $this->getElement($name)) {
- return $element->getValue();
- }
- if ($subForm = $this->getSubForm($name)) {
- return $subForm->getValues(true);
- }
- foreach ($this->getSubForms() as $subForm) {
- if ($name == $subForm->getElementsBelongTo()) {
- return $subForm->getValues(true);
- }
- }
- return null;
- }
- /**
- * Retrieve all form element values
- *
- * @param bool $suppressArrayNotation
- * @return array
- */
- public function getValues($suppressArrayNotation = false)
- {
- $values = array();
- $eBelongTo = null;
- if ($this->isArray()) {
- $eBelongTo = $this->getElementsBelongTo();
- }
- foreach ($this->getElements() as $key => $element) {
- if (!$element->getIgnore()) {
- $merge = array();
- if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
- if ('' !== (string)$belongsTo) {
- $key = $belongsTo . '[' . $key . ']';
- }
- }
- $merge = $this->_attachToArray($element->getValue(), $key);
- $values = $this->_array_replace_recursive($values, $merge);
- }
- }
- foreach ($this->getSubForms() as $key => $subForm) {
- $merge = array();
- if (!$subForm->isArray()) {
- $merge[$key] = $subForm->getValues();
- } else {
- $merge = $this->_attachToArray($subForm->getValues(true),
- $subForm->getElementsBelongTo());
- }
- $values = $this->_array_replace_recursive($values, $merge);
- }
- if (!$suppressArrayNotation &&
- $this->isArray() &&
- !$this->_getIsRendered()) {
- $values = $this->_attachToArray($values, $this->getElementsBelongTo());
- }
- return $values;
- }
- /**
- * Returns only the valid values from the given form input.
- *
- * For models that can be saved in a partially valid state, for example when following the builder,
- * prototype or state patterns it is particularly interessting to retrieve all the current valid
- * values to persist them.
- *
- * @param array $data
- * @param bool $suppressArrayNotation
- * @return array
- */
- public function getValidValues($data, $suppressArrayNotation = false)
- {
- $values = array();
- $eBelongTo = null;
- if ($this->isArray()) {
- $eBelongTo = $this->getElementsBelongTo();
- $data = $this->_dissolveArrayValue($data, $eBelongTo);
- }
- $context = $data;
- foreach ($this->getElements() as $key => $element) {
- if (!$element->getIgnore()) {
- $check = $data;
- if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
- $check = $this->_dissolveArrayValue($data, $belongsTo);
- }
- if (isset($check[$key])) {
- if($element->isValid($check[$key], $context)) {
- $merge = array();
- if ($belongsTo !== $eBelongTo && '' !== (string)$belongsTo) {
- $key = $belongsTo . '[' . $key . ']';
- }
- $merge = $this->_attachToArray($element->getValue(), $key);
- $values = $this->_array_replace_recursive($values, $merge);
- }
- $data = $this->_dissolveArrayUnsetKey($data, $belongsTo, $key);
- }
- }
- }
- foreach ($this->getSubForms() as $key => $form) {
- $merge = array();
- if (isset($data[$key]) && !$form->isArray()) {
- $tmp = $form->getValidValues($data[$key]);
- if (!empty($tmp)) {
- $merge[$key] = $tmp;
- }
- } else {
- $tmp = $form->getValidValues($data, true);
- if (!empty($tmp)) {
- $merge = $this->_attachToArray($tmp, $form->getElementsBelongTo());
- }
- }
- $values = $this->_array_replace_recursive($values, $merge);
- }
- if (!$suppressArrayNotation &&
- $this->isArray() &&
- !empty($values) &&
- !$this->_getIsRendered()) {
- $values = $this->_attachToArray($values, $this->getElementsBelongTo());
- }
- return $values;
- }
- /**
- * Get unfiltered element value
- *
- * @param string $name
- * @return mixed
- */
- public function getUnfilteredValue($name)
- {
- if ($element = $this->getElement($name)) {
- return $element->getUnfilteredValue();
- }
- return null;
- }
- /**
- * Retrieve all unfiltered element values
- *
- * @return array
- */
- public function getUnfilteredValues()
- {
- $values = array();
- foreach ($this->getElements() as $key => $element) {
- $values[$key] = $element->getUnfilteredValue();
- }
- return $values;
- }
- /**
- * Set all elements' filters
- *
- * @param array $filters
- * @return Zend_Form
- */
- public function setElementFilters(array $filters)
- {
- foreach ($this->getElements() as $element) {
- $element->setFilters($filters);
- }
- return $this;
- }
- /**
- * Set name of array elements belong to
- *
- * @param string $array
- * @return Zend_Form
- */
- public function setElementsBelongTo($array)
- {
- $origName = $this->getElementsBelongTo();
- $name = $this->filterName($array, true);
- if ('' === $name) {
- $name = null;
- }
- $this->_elementsBelongTo = $name;
- if (null === $name) {
- $this->setIsArray(false);
- if (null !== $origName) {
- $this->_setElementsBelongTo();
- }
- } else {
- $this->setIsArray(true);
- $this->_setElementsBelongTo();
- }
- return $this;
- }
- /**
- * Set array to which elements belong
- *
- * @param string $name Element name
- * @return void
- */
- protected function _setElementsBelongTo($name = null)
- {
- $array = $this->getElementsBelongTo();
- if (null === $array) {
- return;
- }
- if (null === $name) {
- foreach ($this->getElements() as $element) {
- $element->setBelongsTo($array);
- }
- } else {
- if (null !== ($element = $this->getElement($name))) {
- $element->setBelongsTo($array);
- }
- }
- }
- /**
- * Get name of array elements belong to
- *
- * @return string|null
- */
- public function getElementsBelongTo()
- {
- if ((null === $this->_elementsBelongTo) && $this->isArray()) {
- $name = $this->getName();
- if ('' !== (string)$name) {
- return $name;
- }
- }
- return $this->_elementsBelongTo;
- }
- /**
- * Set flag indicating elements belong to array
- *
- * @param bool $flag Value of flag
- * @return Zend_Form
- */
- public function setIsArray($flag)
- {
- $this->_isArray = (bool) $flag;
- return $this;
- }
- /**
- * Get flag indicating if elements belong to an array
- *
- * @return bool
- */
- public function isArray()
- {
- return $this->_isArray;
- }
- // Element groups:
- /**
- * Add a form group/subform
- *
- * @param Zend_Form $form
- * @param string $name
- * @param int $order
- * @return Zend_Form
- */
- public function addSubForm(Zend_Form $form, $name, $order = null)
- {
- $name = (string) $name;
- foreach ($this->_loaders as $type => $loader) {
- $loaderPaths = $loader->getPaths();
- foreach ($loaderPaths as $prefix => $paths) {
- foreach ($paths as $path) {
- $form->addPrefixPath($prefix, $path, $type);
- }
- }
- }
- if (!empty($this->_elementPrefixPaths)) {
- foreach ($this->_elementPrefixPaths as $spec) {
- list($prefix, $path, $type) = array_values($spec);
- $form->addElementPrefixPath($prefix, $path, $type);
- }
- }
- if (!empty($this->_displayGroupPrefixPaths)) {
- foreach ($this->_displayGroupPrefixPaths as $spec) {
- list($prefix, $path) = array_values($spec);
- $form->addDisplayGroupPrefixPath($prefix, $path);
- }
- }
- if (null !== $order) {
- $form->setOrder($order);
- }
- if (($oldName = $form->getName()) &&
- $oldName !== $name &&
- $oldName === $form->getElementsBelongTo()) {
- $form->setElementsBelongTo($name);
- }
- $form->setName($name);
- $this->_subForms[$name] = $form;
- $this->_order[$name] = $order;
- $this->_orderUpdated = true;
- return $this;
- }
- /**
- * Add multiple form subForms/subforms at once
- *
- * @param array $subForms
- * @return Zend_Form
- */
- public function addSubForms(array $subForms)
- {
- foreach ($subForms as $key => $spec) {
- $name = null;
- if (!is_numeric($key)) {
- $name = $key;
- }
- if ($spec instanceof Zend_Form) {
- $this->addSubForm($spec, $name);
- continue;
- }
- if (is_array($spec)) {
- $argc = count($spec);
- $order = null;
- switch ($argc) {
- case 0:
- continue;
- case (1 <= $argc):
- $subForm = array_shift($spec);
- case (2 <= $argc):
- $name = array_shift($spec);
- case (3 <= $argc):
- $order = array_shift($spec);
- default:
- $this->addSubForm($subForm, $name, $order);
- }
- }
- }
- return $this;
- }
- /**
- * Set multiple form subForms/subforms (overwrites)
- *
- * @param array $subForms
- * @return Zend_Form
- */
- public function setSubForms(array $subForms)
- {
- $this->clearSubForms();
- return $this->addSubForms($subForms);
- }
- /**
- * Retrieve a form subForm/subform
- *
- * @param string $name
- * @return Zend_Form|null
- */
- public function getSubForm($name)
- {
- $name = (string) $name;
- if (isset($this->_subForms[$name])) {
- return $this->_subForms[$name];
- }
- return null;
- }
- /**
- * Retrieve all form subForms/subforms
- *
- * @return array
- */
- public function getSubForms()
- {
- return $this->_subForms;
- }
- /**
- * Remove form subForm/subform
- *
- * @param string $name
- * @return boolean
- */
- public function removeSubForm($name)
- {
- $name = (string) $name;
- if (array_key_exists($name, $this->_subForms)) {
- unset($this->_subForms[$name]);
- if (array_key_exists($name, $this->_order)) {
- unset($this->_order[$name]);
- $this->_orderUpdated = true;
- }
- return true;
- }
- return false;
- }
- /**
- * Remove all form subForms/subforms
- *
- * @return Zend_Form
- */
- public function clearSubForms()
- {
- foreach (array_keys($this->_subForms) as $key) {
- if (array_key_exists($key, $this->_order)) {
- unset($this->_order[$key]);
- }
- }
- $this->_subForms = array();
- $this->_orderUpdated = true;
- return $this;
- }
- // Display groups:
- /**
- * Set default display group class
- *
- * @param string $class
- * @return Zend_Form
- */
- public function setDefaultDisplayGroupClass($class)
- {
- $this->_defaultDisplayGroupClass = (string) $class;
- return $this;
- }
- /**
- * Retrieve default display group class
- *
- * @return string
- */
- public function getDefaultDisplayGroupClass()
- {
- return $this->_defaultDisplayGroupClass;
- }
- /**
- * Add a display group
- *
- * Groups named elements for display purposes.
- *
- * If a referenced element does not yet exist in the form, it is omitted.
- *
- * @param array $elements
- * @param string $name
- * @param array|Zend_Config $options
- * @return Zend_Form
- * @throws Zend_Form_Exception if no valid elements provided
- */
- public function addDisplayGroup(array $elements, $name, $options = null)
- {
- $group = array();
- foreach ($elements as $element) {
- if($element instanceof Zend_Form_Element) {
- $elementName = $element->getName();
- if (!isset($this->_elements[$elementName])) {
- $this->addElement($element);
- }
- $element = $elementName;
- }
- if (isset($this->_elements[$element])) {
- $add = $this->getElement($element);
- if (null !== $add) {
- $group[] = $add;
- }
- }
- }
- if (empty($group)) {
- require_once 'Zend/Form/Exception.php';
- throw new Zend_Form_Exception('No valid elements specified for display group');
- }
- $name = (string) $name;
- if (is_array($options)) {
- $options['form'] = $this;
- $options['elements'] = $group;
- } elseif ($options instanceof Zend_Config) {
- $options = $options->toArray();
- $options['form'] = $this;
- $options['elements'] = $group;
- } else {
- $options = array(
- 'form' => $this,
- 'elements' => $group,
- );
- }
- if (isset($options['displayGroupClass'])) {
- $class = $options['displayGroupClass'];
- unset($options['displayGroupClass']);
- } else {
- $class = $this->getDefaultDisplayGroupClass();
- }
- if (!class_exists($class)) {
- require_once 'Zend/Loader.php';
- Zend_Loader::loadClass($class);
- }
- $this->_displayGroups[$name] = new $class(
- $name,
- $this->getPluginLoader(self::DECORATOR),
- $options
- );
- if (!em…
Large files files are truncated, but you can click here to view the full file