PageRenderTime 360ms CodeModel.GetById 141ms app.highlight 133ms RepoModel.GetById 68ms app.codeStats 1ms

/src/application/libraries/Zend/Form.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 3397 lines | 1969 code | 338 blank | 1090 comment | 320 complexity | 24ef4f09039a6d501e5cdce48633d9f5 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18 * @license    http://framework.zend.com/license/new-bsd     New BSD License
  19 */
  20
  21/** @see Zend_Validate_Interface */
  22require_once 'Zend/Validate/Interface.php';
  23
  24/**
  25 * Zend_Form
  26 *
  27 * @category   Zend
  28 * @package    Zend_Form
  29 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  30 * @license    http://framework.zend.com/license/new-bsd     New BSD License
  31 * @version    $Id: Form.php 24156 2011-06-27 14:57:44Z ezimuel $
  32 */
  33class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
  34{
  35    /**#@+
  36     * Plugin loader type constants
  37     */
  38    const DECORATOR = 'DECORATOR';
  39    const ELEMENT = 'ELEMENT';
  40    /**#@-*/
  41
  42    /**#@+
  43     * Method type constants
  44     */
  45    const METHOD_DELETE = 'delete';
  46    const METHOD_GET    = 'get';
  47    const METHOD_POST   = 'post';
  48    const METHOD_PUT    = 'put';
  49    /**#@-*/
  50
  51    /**#@+
  52     * Encoding type constants
  53     */
  54    const ENCTYPE_URLENCODED = 'application/x-www-form-urlencoded';
  55    const ENCTYPE_MULTIPART  = 'multipart/form-data';
  56    /**#@-*/
  57
  58    /**
  59     * Form metadata and attributes
  60     * @var array
  61     */
  62    protected $_attribs = array();
  63
  64    /**
  65     * Decorators for rendering
  66     * @var array
  67     */
  68    protected $_decorators = array();
  69
  70    /**
  71     * Default display group class
  72     * @var string
  73     */
  74    protected $_defaultDisplayGroupClass = 'Zend_Form_DisplayGroup';
  75
  76    /**
  77     * Form description
  78     * @var string
  79     */
  80    protected $_description;
  81
  82    /**
  83     * Should we disable loading the default decorators?
  84     * @var bool
  85     */
  86    protected $_disableLoadDefaultDecorators = false;
  87
  88    /**
  89     * Display group prefix paths
  90     * @var array
  91     */
  92    protected $_displayGroupPrefixPaths = array();
  93
  94    /**
  95     * Groups of elements grouped for display purposes
  96     * @var array
  97     */
  98    protected $_displayGroups = array();
  99
 100    /**
 101     * Global decorators to apply to all elements
 102     * @var null|array
 103     */
 104    protected $_elementDecorators;
 105
 106    /**
 107     * Prefix paths to use when creating elements
 108     * @var array
 109     */
 110    protected $_elementPrefixPaths = array();
 111
 112    /**
 113     * Form elements
 114     * @var array
 115     */
 116    protected $_elements = array();
 117
 118    /**
 119     * Array to which elements belong (if any)
 120     * @var string
 121     */
 122    protected $_elementsBelongTo;
 123
 124    /**
 125     * Custom form-level error messages
 126     * @var array
 127     */
 128    protected $_errorMessages = array();
 129
 130    /**
 131     * Are there errors in the form?
 132     * @var bool
 133     */
 134    protected $_errorsExist = false;
 135
 136    /**
 137     * Has the form been manually flagged as an error?
 138     * @var bool
 139     */
 140    protected $_errorsForced = false;
 141
 142    /**
 143     * Form order
 144     * @var int|null
 145     */
 146    protected $_formOrder;
 147
 148    /**
 149     * Whether or not form elements are members of an array
 150     * @var bool
 151     */
 152    protected $_isArray = false;
 153
 154    /**
 155     * Form legend
 156     * @var string
 157     */
 158    protected $_legend;
 159
 160    /**
 161     * Plugin loaders
 162     * @var array
 163     */
 164    protected $_loaders = array();
 165
 166    /**
 167     * Allowed form methods
 168     * @var array
 169     */
 170    protected $_methods = array('delete', 'get', 'post', 'put');
 171
 172    /**
 173     * Order in which to display and iterate elements
 174     * @var array
 175     */
 176    protected $_order = array();
 177
 178    /**
 179     * Whether internal order has been updated or not
 180     * @var bool
 181     */
 182    protected $_orderUpdated = false;
 183
 184    /**
 185     * Sub form prefix paths
 186     * @var array
 187     */
 188    protected $_subFormPrefixPaths = array();
 189
 190    /**
 191     * Sub forms
 192     * @var array
 193     */
 194    protected $_subForms = array();
 195
 196    /**
 197     * @var Zend_Translate
 198     */
 199    protected $_translator;
 200
 201    /**
 202     * Global default translation adapter
 203     * @var Zend_Translate
 204     */
 205    protected static $_translatorDefault;
 206
 207    /**
 208     * is the translator disabled?
 209     * @var bool
 210     */
 211    protected $_translatorDisabled = false;
 212
 213    /**
 214     * @var Zend_View_Interface
 215     */
 216    protected $_view;
 217
 218    /**
 219     * @var bool
 220     */
 221    protected $_isRendered = false;
 222
 223    /**
 224     * Constructor
 225     *
 226     * Registers form view helper as decorator
 227     *
 228     * @param mixed $options
 229     * @return void
 230     */
 231    public function __construct($options = null)
 232    {
 233        if (is_array($options)) {
 234            $this->setOptions($options);
 235        } elseif ($options instanceof Zend_Config) {
 236            $this->setConfig($options);
 237        }
 238
 239        // Extensions...
 240        $this->init();
 241
 242        $this->loadDefaultDecorators();
 243    }
 244
 245    /**
 246     * Clone form object and all children
 247     *
 248     * @return void
 249     */
 250    public function __clone()
 251    {
 252        $elements = array();
 253        foreach ($this->getElements() as $name => $element) {
 254            $elements[] = clone $element;
 255        }
 256        $this->setElements($elements);
 257
 258        $subForms = array();
 259        foreach ($this->getSubForms() as $name => $subForm) {
 260            $subForms[$name] = clone $subForm;
 261        }
 262        $this->setSubForms($subForms);
 263
 264        $displayGroups = array();
 265        foreach ($this->_displayGroups as $group)  {
 266            $clone    = clone $group;
 267            $elements = array();
 268            foreach ($clone->getElements() as $name => $e) {
 269                $elements[] = $this->getElement($name);
 270            }
 271            $clone->setElements($elements);
 272            $displayGroups[] = $clone;
 273        }
 274        $this->setDisplayGroups($displayGroups);
 275    }
 276
 277    /**
 278     * Reset values of form
 279     *
 280     * @return Zend_Form
 281     */
 282    public function reset()
 283    {
 284        foreach ($this->getElements() as $element) {
 285            $element->setValue(null);
 286        }
 287        foreach ($this->getSubForms() as $subForm) {
 288            $subForm->reset();
 289        }
 290
 291        return $this;
 292    }
 293
 294    /**
 295     * Initialize form (used by extending classes)
 296     *
 297     * @return void
 298     */
 299    public function init()
 300    {
 301    }
 302
 303    /**
 304     * Set form state from options array
 305     *
 306     * @param  array $options
 307     * @return Zend_Form
 308     */
 309    public function setOptions(array $options)
 310    {
 311        if (isset($options['prefixPath'])) {
 312            $this->addPrefixPaths($options['prefixPath']);
 313            unset($options['prefixPath']);
 314        }
 315
 316        if (isset($options['elementPrefixPath'])) {
 317            $this->addElementPrefixPaths($options['elementPrefixPath']);
 318            unset($options['elementPrefixPath']);
 319        }
 320
 321        if (isset($options['displayGroupPrefixPath'])) {
 322            $this->addDisplayGroupPrefixPaths($options['displayGroupPrefixPath']);
 323            unset($options['displayGroupPrefixPath']);
 324        }
 325
 326        if (isset($options['elementDecorators'])) {
 327            $this->_elementDecorators = $options['elementDecorators'];
 328            unset($options['elementDecorators']);
 329        }
 330
 331        if (isset($options['elements'])) {
 332            $this->setElements($options['elements']);
 333            unset($options['elements']);
 334        }
 335
 336        if (isset($options['defaultDisplayGroupClass'])) {
 337            $this->setDefaultDisplayGroupClass($options['defaultDisplayGroupClass']);
 338            unset($options['defaultDisplayGroupClass']);
 339        }
 340
 341        if (isset($options['displayGroupDecorators'])) {
 342            $displayGroupDecorators = $options['displayGroupDecorators'];
 343            unset($options['displayGroupDecorators']);
 344        }
 345
 346        if (isset($options['elementsBelongTo'])) {
 347            $elementsBelongTo = $options['elementsBelongTo'];
 348            unset($options['elementsBelongTo']);
 349        }
 350
 351        if (isset($options['attribs'])) {
 352            $this->addAttribs($options['attribs']);
 353            unset($options['attribs']);
 354        }
 355
 356        $forbidden = array(
 357            'Options', 'Config', 'PluginLoader', 'SubForms', 'Translator',
 358            'Attrib', 'Default',
 359        );
 360
 361        foreach ($options as $key => $value) {
 362            $normalized = ucfirst($key);
 363            if (in_array($normalized, $forbidden)) {
 364                continue;
 365            }
 366
 367            $method = 'set' . $normalized;
 368            if (method_exists($this, $method)) {
 369                if($normalized == 'View' && !($value instanceof Zend_View_Interface)) {
 370                    continue;
 371                }
 372                $this->$method($value);
 373            } else {
 374                $this->setAttrib($key, $value);
 375            }
 376        }
 377
 378        if (isset($displayGroupDecorators)) {
 379            $this->setDisplayGroupDecorators($displayGroupDecorators);
 380        }
 381
 382        if (isset($elementsBelongTo)) {
 383            $this->setElementsBelongTo($elementsBelongTo);
 384        }
 385
 386        return $this;
 387    }
 388
 389    /**
 390     * Set form state from config object
 391     *
 392     * @param  Zend_Config $config
 393     * @return Zend_Form
 394     */
 395    public function setConfig(Zend_Config $config)
 396    {
 397        return $this->setOptions($config->toArray());
 398    }
 399
 400
 401    // Loaders
 402
 403    /**
 404     * Set plugin loaders for use with decorators and elements
 405     *
 406     * @param  Zend_Loader_PluginLoader_Interface $loader
 407     * @param  string $type 'decorator' or 'element'
 408     * @return Zend_Form
 409     * @throws Zend_Form_Exception on invalid type
 410     */
 411    public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type = null)
 412    {
 413        $type = strtoupper($type);
 414        switch ($type) {
 415            case self::DECORATOR:
 416            case self::ELEMENT:
 417                $this->_loaders[$type] = $loader;
 418                return $this;
 419            default:
 420                require_once 'Zend/Form/Exception.php';
 421                throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type));
 422        }
 423    }
 424
 425    /**
 426     * Retrieve plugin loader for given type
 427     *
 428     * $type may be one of:
 429     * - decorator
 430     * - element
 431     *
 432     * If a plugin loader does not exist for the given type, defaults are
 433     * created.
 434     *
 435     * @param  string $type
 436     * @return Zend_Loader_PluginLoader_Interface
 437     */
 438    public function getPluginLoader($type = null)
 439    {
 440        $type = strtoupper($type);
 441        if (!isset($this->_loaders[$type])) {
 442            switch ($type) {
 443                case self::DECORATOR:
 444                    $prefixSegment = 'Form_Decorator';
 445                    $pathSegment   = 'Form/Decorator';
 446                    break;
 447                case self::ELEMENT:
 448                    $prefixSegment = 'Form_Element';
 449                    $pathSegment   = 'Form/Element';
 450                    break;
 451                default:
 452                    require_once 'Zend/Form/Exception.php';
 453                    throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
 454            }
 455
 456            require_once 'Zend/Loader/PluginLoader.php';
 457            $this->_loaders[$type] = new Zend_Loader_PluginLoader(
 458                array('Zend_' . $prefixSegment . '_' => 'Zend/' . $pathSegment . '/')
 459            );
 460        }
 461
 462        return $this->_loaders[$type];
 463    }
 464
 465    /**
 466     * Add prefix path for plugin loader
 467     *
 468     * If no $type specified, assumes it is a base path for both filters and
 469     * validators, and sets each according to the following rules:
 470     * - decorators: $prefix = $prefix . '_Decorator'
 471     * - elements: $prefix = $prefix . '_Element'
 472     *
 473     * Otherwise, the path prefix is set on the appropriate plugin loader.
 474     *
 475     * If $type is 'decorator', sets the path in the decorator plugin loader
 476     * for all elements. Additionally, if no $type is provided,
 477     * the prefix and path is added to both decorator and element
 478     * plugin loader with following settings:
 479     * $prefix . '_Decorator', $path . '/Decorator/'
 480     * $prefix . '_Element', $path . '/Element/'
 481     *
 482     * @param  string $prefix
 483     * @param  string $path
 484     * @param  string $type
 485     * @return Zend_Form
 486     * @throws Zend_Form_Exception for invalid type
 487     */
 488    public function addPrefixPath($prefix, $path, $type = null)
 489    {
 490        $type = strtoupper($type);
 491        switch ($type) {
 492            case self::DECORATOR:
 493            case self::ELEMENT:
 494                $loader = $this->getPluginLoader($type);
 495                $loader->addPrefixPath($prefix, $path);
 496                return $this;
 497            case null:
 498                $prefix = rtrim($prefix, '_');
 499                $path   = rtrim($path, DIRECTORY_SEPARATOR);
 500                foreach (array(self::DECORATOR, self::ELEMENT) as $type) {
 501                    $cType        = ucfirst(strtolower($type));
 502                    $pluginPath   = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR;
 503                    $pluginPrefix = $prefix . '_' . $cType;
 504                    $loader       = $this->getPluginLoader($type);
 505                    $loader->addPrefixPath($pluginPrefix, $pluginPath);
 506                }
 507                return $this;
 508            default:
 509                require_once 'Zend/Form/Exception.php';
 510                throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
 511        }
 512    }
 513
 514    /**
 515     * Add many prefix paths at once
 516     *
 517     * @param  array $spec
 518     * @return Zend_Form
 519     */
 520    public function addPrefixPaths(array $spec)
 521    {
 522        if (isset($spec['prefix']) && isset($spec['path'])) {
 523            return $this->addPrefixPath($spec['prefix'], $spec['path']);
 524        }
 525        foreach ($spec as $type => $paths) {
 526            if (is_numeric($type) && is_array($paths)) {
 527                $type = null;
 528                if (isset($paths['prefix']) && isset($paths['path'])) {
 529                    if (isset($paths['type'])) {
 530                        $type = $paths['type'];
 531                    }
 532                    $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
 533                }
 534            } elseif (!is_numeric($type)) {
 535                if (!isset($paths['prefix']) || !isset($paths['path'])) {
 536                    continue;
 537                }
 538                $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
 539            }
 540        }
 541        return $this;
 542    }
 543
 544    /**
 545     * Add prefix path for all elements
 546     *
 547     * @param  string $prefix
 548     * @param  string $path
 549     * @param  string $type
 550     * @return Zend_Form
 551     */
 552    public function addElementPrefixPath($prefix, $path, $type = null)
 553    {
 554        $this->_elementPrefixPaths[] = array(
 555            'prefix' => $prefix,
 556            'path'   => $path,
 557            'type'   => $type,
 558        );
 559
 560        foreach ($this->getElements() as $element) {
 561            $element->addPrefixPath($prefix, $path, $type);
 562        }
 563
 564        foreach ($this->getSubForms() as $subForm) {
 565            $subForm->addElementPrefixPath($prefix, $path, $type);
 566        }
 567
 568        return $this;
 569    }
 570
 571    /**
 572     * Add prefix paths for all elements
 573     *
 574     * @param  array $spec
 575     * @return Zend_Form
 576     */
 577    public function addElementPrefixPaths(array $spec)
 578    {
 579        $this->_elementPrefixPaths = $this->_elementPrefixPaths + $spec;
 580
 581        foreach ($this->getElements() as $element) {
 582            $element->addPrefixPaths($spec);
 583        }
 584
 585        return $this;
 586    }
 587
 588    /**
 589     * Add prefix path for all display groups
 590     *
 591     * @param  string $prefix
 592     * @param  string $path
 593     * @return Zend_Form
 594     */
 595    public function addDisplayGroupPrefixPath($prefix, $path)
 596    {
 597        $this->_displayGroupPrefixPaths[] = array(
 598            'prefix' => $prefix,
 599            'path'   => $path,
 600        );
 601
 602        foreach ($this->getDisplayGroups() as $group) {
 603            $group->addPrefixPath($prefix, $path);
 604        }
 605
 606        return $this;
 607    }
 608
 609    /**
 610     * Add multiple display group prefix paths at once
 611     *
 612     * @param  array $spec
 613     * @return Zend_Form
 614     */
 615    public function addDisplayGroupPrefixPaths(array $spec)
 616    {
 617        foreach ($spec as $key => $value) {
 618            if (is_string($value) && !is_numeric($key)) {
 619                $this->addDisplayGroupPrefixPath($key, $value);
 620                continue;
 621            }
 622
 623            if (is_string($value) && is_numeric($key)) {
 624                continue;
 625            }
 626
 627            if (is_array($value)) {
 628                $count = count($value);
 629                if (array_keys($value) === range(0, $count - 1)) {
 630                    if ($count < 2) {
 631                        continue;
 632                    }
 633                    $prefix = array_shift($value);
 634                    $path   = array_shift($value);
 635                    $this->addDisplayGroupPrefixPath($prefix, $path);
 636                    continue;
 637                }
 638                if (array_key_exists('prefix', $value) && array_key_exists('path', $value)) {
 639                    $this->addDisplayGroupPrefixPath($value['prefix'], $value['path']);
 640                }
 641            }
 642        }
 643        return $this;
 644    }
 645
 646    // Form metadata:
 647
 648    /**
 649     * Set form attribute
 650     *
 651     * @param  string $key
 652     * @param  mixed $value
 653     * @return Zend_Form
 654     */
 655    public function setAttrib($key, $value)
 656    {
 657        $key = (string) $key;
 658        $this->_attribs[$key] = $value;
 659        return $this;
 660    }
 661
 662    /**
 663     * Add multiple form attributes at once
 664     *
 665     * @param  array $attribs
 666     * @return Zend_Form
 667     */
 668    public function addAttribs(array $attribs)
 669    {
 670        foreach ($attribs as $key => $value) {
 671            $this->setAttrib($key, $value);
 672        }
 673        return $this;
 674    }
 675
 676    /**
 677     * Set multiple form attributes at once
 678     *
 679     * Overwrites any previously set attributes.
 680     *
 681     * @param  array $attribs
 682     * @return Zend_Form
 683     */
 684    public function setAttribs(array $attribs)
 685    {
 686        $this->clearAttribs();
 687        return $this->addAttribs($attribs);
 688    }
 689
 690    /**
 691     * Retrieve a single form attribute
 692     *
 693     * @param  string $key
 694     * @return mixed
 695     */
 696    public function getAttrib($key)
 697    {
 698        $key = (string) $key;
 699        if (!isset($this->_attribs[$key])) {
 700            return null;
 701        }
 702
 703        return $this->_attribs[$key];
 704    }
 705
 706    /**
 707     * Retrieve all form attributes/metadata
 708     *
 709     * @return array
 710     */
 711    public function getAttribs()
 712    {
 713        return $this->_attribs;
 714    }
 715
 716    /**
 717     * Remove attribute
 718     *
 719     * @param  string $key
 720     * @return bool
 721     */
 722    public function removeAttrib($key)
 723    {
 724        if (isset($this->_attribs[$key])) {
 725            unset($this->_attribs[$key]);
 726            return true;
 727        }
 728
 729        return false;
 730    }
 731
 732    /**
 733     * Clear all form attributes
 734     *
 735     * @return Zend_Form
 736     */
 737    public function clearAttribs()
 738    {
 739        $this->_attribs = array();
 740        return $this;
 741    }
 742
 743    /**
 744     * Set form action
 745     *
 746     * @param  string $action
 747     * @return Zend_Form
 748     */
 749    public function setAction($action)
 750    {
 751        return $this->setAttrib('action', (string) $action);
 752    }
 753
 754    /**
 755     * Get form action
 756     *
 757     * Sets default to '' if not set.
 758     *
 759     * @return string
 760     */
 761    public function getAction()
 762    {
 763        $action = $this->getAttrib('action');
 764        if (null === $action) {
 765            $action = '';
 766            $this->setAction($action);
 767        }
 768        return $action;
 769    }
 770
 771    /**
 772     * Set form method
 773     *
 774     * Only values in {@link $_methods()} allowed
 775     *
 776     * @param  string $method
 777     * @return Zend_Form
 778     * @throws Zend_Form_Exception
 779     */
 780    public function setMethod($method)
 781    {
 782        $method = strtolower($method);
 783        if (!in_array($method, $this->_methods)) {
 784            require_once 'Zend/Form/Exception.php';
 785            throw new Zend_Form_Exception(sprintf('"%s" is an invalid form method', $method));
 786        }
 787        $this->setAttrib('method', $method);
 788        return $this;
 789    }
 790
 791    /**
 792     * Retrieve form method
 793     *
 794     * @return string
 795     */
 796    public function getMethod()
 797    {
 798        if (null === ($method = $this->getAttrib('method'))) {
 799            $method = self::METHOD_POST;
 800            $this->setAttrib('method', $method);
 801        }
 802        return strtolower($method);
 803    }
 804
 805    /**
 806     * Set encoding type
 807     *
 808     * @param  string $value
 809     * @return Zend_Form
 810     */
 811    public function setEnctype($value)
 812    {
 813        $this->setAttrib('enctype', $value);
 814        return $this;
 815    }
 816
 817    /**
 818     * Get encoding type
 819     *
 820     * @return string
 821     */
 822    public function getEnctype()
 823    {
 824        if (null === ($enctype = $this->getAttrib('enctype'))) {
 825            $enctype = self::ENCTYPE_URLENCODED;
 826            $this->setAttrib('enctype', $enctype);
 827        }
 828        return $this->getAttrib('enctype');
 829    }
 830
 831    /**
 832     * Filter a name to only allow valid variable characters
 833     *
 834     * @param  string $value
 835     * @param  bool $allowBrackets
 836     * @return string
 837     */
 838    public function filterName($value, $allowBrackets = false)
 839    {
 840        $charset = '^a-zA-Z0-9_\x7f-\xff';
 841        if ($allowBrackets) {
 842            $charset .= '\[\]';
 843        }
 844        return preg_replace('/[' . $charset . ']/', '', (string) $value);
 845    }
 846
 847    /**
 848     * Set form name
 849     *
 850     * @param  string $name
 851     * @return Zend_Form
 852     */
 853    public function setName($name)
 854    {
 855        $name = $this->filterName($name);
 856        if ('' === (string)$name) {
 857            require_once 'Zend/Form/Exception.php';
 858            throw new Zend_Form_Exception('Invalid name provided; must contain only valid variable characters and be non-empty');
 859        }
 860
 861        return $this->setAttrib('name', $name);
 862    }
 863
 864    /**
 865     * Get name attribute
 866     *
 867     * @return null|string
 868     */
 869    public function getName()
 870    {
 871        return $this->getAttrib('name');
 872    }
 873
 874    /**
 875     * Get fully qualified name
 876     *
 877     * Places name as subitem of array and/or appends brackets.
 878     *
 879     * @return string
 880     */
 881    public function getFullyQualifiedName()
 882    {
 883        return $this->getName();
 884    }
 885
 886    /**
 887     * Get element id
 888     *
 889     * @return string
 890     */
 891    public function getId()
 892    {
 893        if (null !== ($id = $this->getAttrib('id'))) {
 894            return $id;
 895        }
 896
 897        $id = $this->getFullyQualifiedName();
 898
 899        // Bail early if no array notation detected
 900        if (!strstr($id, '[')) {
 901            return $id;
 902        }
 903
 904        // Strip array notation
 905        if ('[]' == substr($id, -2)) {
 906            $id = substr($id, 0, strlen($id) - 2);
 907        }
 908        $id = str_replace('][', '-', $id);
 909        $id = str_replace(array(']', '['), '-', $id);
 910        $id = trim($id, '-');
 911
 912        return $id;
 913    }
 914
 915    /**
 916     * Set form legend
 917     *
 918     * @param  string $value
 919     * @return Zend_Form
 920     */
 921    public function setLegend($value)
 922    {
 923        $this->_legend = (string) $value;
 924        return $this;
 925    }
 926
 927    /**
 928     * Get form legend
 929     *
 930     * @return string
 931     */
 932    public function getLegend()
 933    {
 934        return $this->_legend;
 935    }
 936
 937    /**
 938     * Set form description
 939     *
 940     * @param  string $value
 941     * @return Zend_Form
 942     */
 943    public function setDescription($value)
 944    {
 945        $this->_description = (string) $value;
 946        return $this;
 947    }
 948
 949    /**
 950     * Retrieve form description
 951     *
 952     * @return string
 953     */
 954    public function getDescription()
 955    {
 956        return $this->_description;
 957    }
 958
 959    /**
 960     * Set form order
 961     *
 962     * @param  int $index
 963     * @return Zend_Form
 964     */
 965    public function setOrder($index)
 966    {
 967        $this->_formOrder = (int) $index;
 968        return $this;
 969    }
 970
 971    /**
 972     * Get form order
 973     *
 974     * @return int|null
 975     */
 976    public function getOrder()
 977    {
 978        return $this->_formOrder;
 979    }
 980
 981    /**
 982     * When calling renderFormElements or render this method
 983     * is used to set $_isRendered member to prevent repeatedly
 984     * merging belongsTo setting
 985     */
 986    protected function _setIsRendered()
 987    {
 988        $this->_isRendered = true;
 989        return $this;
 990    }
 991
 992    /**
 993     * Get the value of $_isRendered member
 994     */
 995    protected function _getIsRendered()
 996    {
 997        return (bool)$this->_isRendered;
 998    }
 999
1000    // Element interaction:
1001
1002    /**
1003     * Add a new element
1004     *
1005     * $element may be either a string element type, or an object of type
1006     * Zend_Form_Element. If a string element type is provided, $name must be
1007     * provided, and $options may be optionally provided for configuring the
1008     * element.
1009     *
1010     * If a Zend_Form_Element is provided, $name may be optionally provided,
1011     * and any provided $options will be ignored.
1012     *
1013     * @param  string|Zend_Form_Element $element
1014     * @param  string $name
1015     * @param  array|Zend_Config $options
1016     * @throws Zend_Form_Exception on invalid element
1017     * @return Zend_Form
1018     */
1019    public function addElement($element, $name = null, $options = null)
1020    {
1021        if (is_string($element)) {
1022            if (null === $name) {
1023                require_once 'Zend/Form/Exception.php';
1024                throw new Zend_Form_Exception('Elements specified by string must have an accompanying name');
1025            }
1026
1027            if (is_array($this->_elementDecorators)) {
1028                if (null === $options) {
1029                    $options = array('decorators' => $this->_elementDecorators);
1030                } elseif ($options instanceof Zend_Config) {
1031                    $options = $options->toArray();
1032                }
1033                if (is_array($options)
1034                    && !array_key_exists('decorators', $options)
1035                ) {
1036                    $options['decorators'] = $this->_elementDecorators;
1037                }
1038            }
1039
1040            $this->_elements[$name] = $this->createElement($element, $name, $options);
1041        } elseif ($element instanceof Zend_Form_Element) {
1042            $prefixPaths              = array();
1043            $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
1044            if (!empty($this->_elementPrefixPaths)) {
1045                $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
1046            }
1047
1048            if (null === $name) {
1049                $name = $element->getName();
1050            }
1051
1052            $this->_elements[$name] = $element;
1053            $this->_elements[$name]->addPrefixPaths($prefixPaths);
1054        } else {
1055            require_once 'Zend/Form/Exception.php';
1056            throw new Zend_Form_Exception('Element must be specified by string or Zend_Form_Element instance');
1057        }
1058
1059        $this->_order[$name] = $this->_elements[$name]->getOrder();
1060        $this->_orderUpdated = true;
1061        $this->_setElementsBelongTo($name);
1062
1063        return $this;
1064    }
1065
1066    /**
1067     * Create an element
1068     *
1069     * Acts as a factory for creating elements. Elements created with this
1070     * method will not be attached to the form, but will contain element
1071     * settings as specified in the form object (including plugin loader
1072     * prefix paths, default decorators, etc.).
1073     *
1074     * @param  string $type
1075     * @param  string $name
1076     * @param  array|Zend_Config $options
1077     * @return Zend_Form_Element
1078     */
1079    public function createElement($type, $name, $options = null)
1080    {
1081        if (!is_string($type)) {
1082            require_once 'Zend/Form/Exception.php';
1083            throw new Zend_Form_Exception('Element type must be a string indicating type');
1084        }
1085
1086        if (!is_string($name)) {
1087            require_once 'Zend/Form/Exception.php';
1088            throw new Zend_Form_Exception('Element name must be a string');
1089        }
1090
1091        $prefixPaths              = array();
1092        $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
1093        if (!empty($this->_elementPrefixPaths)) {
1094            $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
1095        }
1096
1097        if ($options instanceof Zend_Config) {
1098            $options = $options->toArray();
1099        }
1100
1101        if ((null === $options) || !is_array($options)) {
1102            $options = array('prefixPath' => $prefixPaths);
1103        } elseif (is_array($options)) {
1104            if (array_key_exists('prefixPath', $options)) {
1105                $options['prefixPath'] = array_merge($prefixPaths, $options['prefixPath']);
1106            } else {
1107                $options['prefixPath'] = $prefixPaths;
1108            }
1109        }
1110
1111        $class = $this->getPluginLoader(self::ELEMENT)->load($type);
1112        $element = new $class($name, $options);
1113
1114        return $element;
1115    }
1116
1117    /**
1118     * Add multiple elements at once
1119     *
1120     * @param  array $elements
1121     * @return Zend_Form
1122     */
1123    public function addElements(array $elements)
1124    {
1125        foreach ($elements as $key => $spec) {
1126            $name = null;
1127            if (!is_numeric($key)) {
1128                $name = $key;
1129            }
1130
1131            if (is_string($spec) || ($spec instanceof Zend_Form_Element)) {
1132                $this->addElement($spec, $name);
1133                continue;
1134            }
1135
1136            if (is_array($spec)) {
1137                $argc = count($spec);
1138                $options = array();
1139                if (isset($spec['type'])) {
1140                    $type = $spec['type'];
1141                    if (isset($spec['name'])) {
1142                        $name = $spec['name'];
1143                    }
1144                    if (isset($spec['options'])) {
1145                        $options = $spec['options'];
1146                    }
1147                    $this->addElement($type, $name, $options);
1148                } else {
1149                    switch ($argc) {
1150                        case 0:
1151                            continue;
1152                        case (1 <= $argc):
1153                            $type = array_shift($spec);
1154                        case (2 <= $argc):
1155                            if (null === $name) {
1156                                $name = array_shift($spec);
1157                            } else {
1158                                $options = array_shift($spec);
1159                            }
1160                        case (3 <= $argc):
1161                            if (empty($options)) {
1162                                $options = array_shift($spec);
1163                            }
1164                        default:
1165                            $this->addElement($type, $name, $options);
1166                    }
1167                }
1168            }
1169        }
1170        return $this;
1171    }
1172
1173    /**
1174     * Set form elements (overwrites existing elements)
1175     *
1176     * @param  array $elements
1177     * @return Zend_Form
1178     */
1179    public function setElements(array $elements)
1180    {
1181        $this->clearElements();
1182        return $this->addElements($elements);
1183    }
1184
1185    /**
1186     * Retrieve a single element
1187     *
1188     * @param  string $name
1189     * @return Zend_Form_Element|null
1190     */
1191    public function getElement($name)
1192    {
1193        if (array_key_exists($name, $this->_elements)) {
1194            return $this->_elements[$name];
1195        }
1196        return null;
1197    }
1198
1199    /**
1200     * Retrieve all elements
1201     *
1202     * @return array
1203     */
1204    public function getElements()
1205    {
1206        return $this->_elements;
1207    }
1208
1209    /**
1210     * Remove element
1211     *
1212     * @param  string $name
1213     * @return boolean
1214     */
1215    public function removeElement($name)
1216    {
1217        $name = (string) $name;
1218        if (isset($this->_elements[$name])) {
1219            unset($this->_elements[$name]);
1220            if (array_key_exists($name, $this->_order)) {
1221                unset($this->_order[$name]);
1222                $this->_orderUpdated = true;
1223            } else {
1224                foreach ($this->_displayGroups as $group) {
1225                    if (null !== $group->getElement($name)) {
1226                        $group->removeElement($name);
1227                    }
1228                }
1229            }
1230            return true;
1231        }
1232
1233        return false;
1234    }
1235
1236    /**
1237     * Remove all form elements
1238     *
1239     * @return Zend_Form
1240     */
1241    public function clearElements()
1242    {
1243        foreach (array_keys($this->_elements) as $key) {
1244            if (array_key_exists($key, $this->_order)) {
1245                unset($this->_order[$key]);
1246            }
1247        }
1248        $this->_elements     = array();
1249        $this->_orderUpdated = true;
1250        return $this;
1251    }
1252
1253    /**
1254     * Set default values for elements
1255     *
1256     * Sets values for all elements specified in the array of $defaults.
1257     *
1258     * @param  array $defaults
1259     * @return Zend_Form
1260     */
1261    public function setDefaults(array $defaults)
1262    {
1263        $eBelongTo = null;
1264
1265        if ($this->isArray()) {
1266            $eBelongTo = $this->getElementsBelongTo();
1267            $defaults = $this->_dissolveArrayValue($defaults, $eBelongTo);
1268        }
1269        foreach ($this->getElements() as $name => $element) {
1270            $check = $defaults;
1271            if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
1272                $check = $this->_dissolveArrayValue($defaults, $belongsTo);
1273            }
1274            if (array_key_exists($name, $check)) {
1275                $this->setDefault($name, $check[$name]);
1276                $defaults = $this->_dissolveArrayUnsetKey($defaults, $belongsTo, $name);
1277            }
1278        }
1279        foreach ($this->getSubForms() as $name => $form) {
1280            if (!$form->isArray() && array_key_exists($name, $defaults)) {
1281                $form->setDefaults($defaults[$name]);
1282            } else {
1283                $form->setDefaults($defaults);
1284            }
1285        }
1286        return $this;
1287    }
1288
1289    /**
1290     * Set default value for an element
1291     *
1292     * @param  string $name
1293     * @param  mixed $value
1294     * @return Zend_Form
1295     */
1296    public function setDefault($name, $value)
1297    {
1298        $name = (string) $name;
1299        if ($element = $this->getElement($name)) {
1300            $element->setValue($value);
1301        } else {
1302            if (is_scalar($value)) {
1303                foreach ($this->getSubForms() as $subForm) {
1304                    $subForm->setDefault($name, $value);
1305                }
1306            } elseif (is_array($value) && ($subForm = $this->getSubForm($name))) {
1307                $subForm->setDefaults($value);
1308            }
1309        }
1310        return $this;
1311    }
1312
1313    /**
1314     * Retrieve value for single element
1315     *
1316     * @param  string $name
1317     * @return mixed
1318     */
1319    public function getValue($name)
1320    {
1321        if ($element = $this->getElement($name)) {
1322            return $element->getValue();
1323        }
1324
1325        if ($subForm = $this->getSubForm($name)) {
1326            return $subForm->getValues(true);
1327        }
1328
1329        foreach ($this->getSubForms() as $subForm) {
1330            if ($name == $subForm->getElementsBelongTo()) {
1331                return $subForm->getValues(true);
1332            }
1333        }
1334        return null;
1335    }
1336
1337    /**
1338     * Retrieve all form element values
1339     *
1340     * @param  bool $suppressArrayNotation
1341     * @return array
1342     */
1343    public function getValues($suppressArrayNotation = false)
1344    {
1345        $values = array();
1346        $eBelongTo = null;
1347
1348        if ($this->isArray()) {
1349            $eBelongTo = $this->getElementsBelongTo();
1350        }
1351
1352        foreach ($this->getElements() as $key => $element) {
1353            if (!$element->getIgnore()) {
1354                $merge = array();
1355                if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
1356                    if ('' !== (string)$belongsTo) {
1357                        $key = $belongsTo . '[' . $key . ']';
1358                    }
1359                }
1360                $merge = $this->_attachToArray($element->getValue(), $key);
1361                $values = $this->_array_replace_recursive($values, $merge);
1362            }
1363        }
1364        foreach ($this->getSubForms() as $key => $subForm) {
1365            $merge = array();
1366            if (!$subForm->isArray()) {
1367                $merge[$key] = $subForm->getValues();
1368            } else {
1369                $merge = $this->_attachToArray($subForm->getValues(true),
1370                                               $subForm->getElementsBelongTo());
1371            }
1372            $values = $this->_array_replace_recursive($values, $merge);
1373        }
1374
1375        if (!$suppressArrayNotation &&
1376            $this->isArray() &&
1377            !$this->_getIsRendered()) {
1378            $values = $this->_attachToArray($values, $this->getElementsBelongTo());
1379        }
1380
1381        return $values;
1382    }
1383
1384    /**
1385     * Returns only the valid values from the given form input.
1386     *
1387     * For models that can be saved in a partially valid state, for example when following the builder,
1388     * prototype or state patterns it is particularly interessting to retrieve all the current valid
1389     * values to persist them.
1390     *
1391     * @param  array $data
1392     * @param  bool $suppressArrayNotation
1393     * @return array
1394     */
1395    public function getValidValues($data, $suppressArrayNotation = false)
1396    {
1397        $values = array();
1398        $eBelongTo = null;
1399
1400        if ($this->isArray()) {
1401            $eBelongTo = $this->getElementsBelongTo();
1402            $data = $this->_dissolveArrayValue($data, $eBelongTo);
1403        }
1404        $context = $data;
1405        foreach ($this->getElements() as $key => $element) {
1406            if (!$element->getIgnore()) {
1407                $check = $data;
1408                if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
1409                    $check = $this->_dissolveArrayValue($data, $belongsTo);
1410                }
1411                if (isset($check[$key])) {
1412                    if($element->isValid($check[$key], $context)) {
1413                        $merge = array();
1414                        if ($belongsTo !== $eBelongTo && '' !== (string)$belongsTo) {
1415                            $key = $belongsTo . '[' . $key . ']';
1416                        }
1417                        $merge = $this->_attachToArray($element->getValue(), $key);
1418                        $values = $this->_array_replace_recursive($values, $merge);
1419                    }
1420                    $data = $this->_dissolveArrayUnsetKey($data, $belongsTo, $key);
1421                }
1422            }
1423        }
1424        foreach ($this->getSubForms() as $key => $form) {
1425            $merge = array();
1426            if (isset($data[$key]) && !$form->isArray()) {
1427                $tmp = $form->getValidValues($data[$key]);
1428                if (!empty($tmp)) {
1429                    $merge[$key] = $tmp;
1430                }
1431            } else {
1432                $tmp = $form->getValidValues($data, true);
1433                if (!empty($tmp)) {
1434                    $merge = $this->_attachToArray($tmp, $form->getElementsBelongTo());
1435                }
1436            }
1437            $values = $this->_array_replace_recursive($values, $merge);
1438        }
1439        if (!$suppressArrayNotation &&
1440            $this->isArray() &&
1441            !empty($values) &&
1442            !$this->_getIsRendered()) {
1443            $values = $this->_attachToArray($values, $this->getElementsBelongTo());
1444        }
1445
1446        return $values;
1447    }
1448
1449    /**
1450     * Get unfiltered element value
1451     *
1452     * @param  string $name
1453     * @return mixed
1454     */
1455    public function getUnfilteredValue($name)
1456    {
1457        if ($element = $this->getElement($name)) {
1458            return $element->getUnfilteredValue();
1459        }
1460        return null;
1461    }
1462
1463    /**
1464     * Retrieve all unfiltered element values
1465     *
1466     * @return array
1467     */
1468    public function getUnfilteredValues()
1469    {
1470        $values = array();
1471        foreach ($this->getElements() as $key => $element) {
1472            $values[$key] = $element->getUnfilteredValue();
1473        }
1474
1475        return $values;
1476    }
1477
1478    /**
1479     * Set all elements' filters
1480     *
1481     * @param  array $filters
1482     * @return Zend_Form
1483     */
1484    public function setElementFilters(array $filters)
1485    {
1486        foreach ($this->getElements() as $element) {
1487            $element->setFilters($filters);
1488        }
1489        return $this;
1490    }
1491
1492    /**
1493     * Set name of array elements belong to
1494     *
1495     * @param  string $array
1496     * @return Zend_Form
1497     */
1498    public function setElementsBelongTo($array)
1499    {
1500        $origName = $this->getElementsBelongTo();
1501        $name = $this->filterName($array, true);
1502        if ('' === $name) {
1503            $name = null;
1504        }
1505        $this->_elementsBelongTo = $name;
1506
1507        if (null === $name) {
1508            $this->setIsArray(false);
1509            if (null !== $origName) {
1510                $this->_setElementsBelongTo();
1511            }
1512        } else {
1513            $this->setIsArray(true);
1514            $this->_setElementsBelongTo();
1515        }
1516
1517        return $this;
1518    }
1519
1520    /**
1521     * Set array to which elements belong
1522     *
1523     * @param  string $name Element name
1524     * @return void
1525     */
1526    protected function _setElementsBelongTo($name = null)
1527    {
1528        $array = $this->getElementsBelongTo();
1529
1530        if (null === $array) {
1531            return;
1532        }
1533
1534        if (null === $name) {
1535            foreach ($this->getElements() as $element) {
1536                $element->setBelongsTo($array);
1537            }
1538        } else {
1539            if (null !== ($element = $this->getElement($name))) {
1540                $element->setBelongsTo($array);
1541            }
1542        }
1543    }
1544
1545    /**
1546     * Get name of array elements belong to
1547     *
1548     * @return string|null
1549     */
1550    public function getElementsBelongTo()
1551    {
1552        if ((null === $this->_elementsBelongTo) && $this->isArray()) {
1553            $name = $this->getName();
1554            if ('' !== (string)$name) {
1555                return $name;
1556            }
1557        }
1558        return $this->_elementsBelongTo;
1559    }
1560
1561    /**
1562     * Set flag indicating elements belong to array
1563     *
1564     * @param  bool $flag Value of flag
1565     * @return Zend_Form
1566     */
1567    public function setIsArray($flag)
1568    {
1569        $this->_isArray = (bool) $flag;
1570        return $this;
1571    }
1572
1573    /**
1574     * Get flag indicating if elements belong to an array
1575     *
1576     * @return bool
1577     */
1578    public function isArray()
1579    {
1580        return $this->_isArray;
1581    }
1582
1583    // Element groups:
1584
1585    /**
1586     * Add a form group/subform
1587     *
1588     * @param  Zend_Form $form
1589     * @param  string $name
1590     * @param  int $order
1591     * @return Zend_Form
1592     */
1593    public function addSubForm(Zend_Form $form, $name, $order = null)
1594    {
1595        $name = (string) $name;
1596        foreach ($this->_loaders as $type => $loader) {
1597            $loaderPaths = $loader->getPaths();
1598            foreach ($loaderPaths as $prefix => $paths) {
1599                foreach ($paths as $path) {
1600                    $form->addPrefixPath($prefix, $path, $type);
1601                }
1602            }
1603        }
1604
1605        if (!empty($this->_elementPrefixPaths)) {
1606            foreach ($this->_elementPrefixPaths as $spec) {
1607                list($prefix, $path, $type) = array_values($spec);
1608                $form->addElementPrefixPath($prefix, $path, $type);
1609            }
1610        }
1611
1612        if (!empty($this->_displayGroupPrefixPaths)) {
1613            foreach ($this->_displayGroupPrefixPaths as $spec) {
1614                list($prefix, $path) = array_values($spec);
1615                $form->addDisplayGroupPrefixPath($prefix, $path);
1616            }
1617        }
1618
1619        if (null !== $order) {
1620            $form->setOrder($order);
1621        }
1622
1623        if (($oldName = $form->getName()) &&
1624            $oldName !== $name &&
1625            $oldName === $form->getElementsBelongTo()) {
1626            $form->setElementsBelongTo($name);
1627        }
1628
1629        $form->setName($name);
1630        $this->_subForms[$name] = $form;
1631        $this->_order[$name]    = $order;
1632        $this->_orderUpdated    = true;
1633        return $this;
1634    }
1635
1636    /**
1637     * Add multiple form subForms/subforms at once
1638     *
1639     * @param  array $subForms
1640     * @return Zend_Form
1641     */
1642    public function addSubForms(array $subForms)
1643    {
1644        foreach ($subForms as $key => $spec) {          
1645            $name= (string) $key;
1646            if ($spec instanceof Zend_Form) {
1647                $this->addSubForm($spec, $name);
1648                continue;
1649            }
1650
1651            if (is_array($spec)) {
1652                $argc  = count($spec);
1653                $order = null;
1654                switch ($argc) {
1655                    case 0:
1656                        continue;
1657                    case (1 <= $argc):
1658                        $subForm = array_shift($spec);
1659                    case (2 <= $argc):
1660                        $name  = array_shift($spec);
1661                    case (3 <= $argc):
1662                        $order = array_shift($spec);
1663                    default:
1664                        $this->addSubForm($subForm, $name, $order);
1665                }
1666            }
1667        }
1668        return $this;
1669    }
1670
1671    /**
1672     * Set multiple form subForms/subforms (overwrites)
1673     *
1674     * @param  array $subForms
1675     * @return Zend_Form
1676     */
1677    public function setSubForms(array $subForms)
1678    {
1679        $this->clearSubForms();
1680        return $this->addSubForms($subForms);
1681    }
1682
1683    /**
1684     * Retrieve a form subForm/subform
1685     *
1686     * @param  string $name
1687     * @return Zend_Form|null
1688     */
1689    public function getSubForm($name)
1690    {
1691        $name = (string) $name;
1692        if (isset($this->_subForms[$name])) {
1693            return $this->_subForms[$name];
1694        }
1695        return null;
1696    }
1697
1698    /**
1699     * Retrieve all form subForms/subforms
1700     *
1701     * @return array
1702     */
1703    public function getSubForms()
1704    {
1705        return $this->_subForms;
1706    }
1707
1708    /**
1709     * Remove form subForm/subform
1710     *
1711     * @param  string $name
1712     * @return boolean
1713     */
1714    public function removeSubForm($name)
1715    {
1716        $name = (string) $name;
1717        if (array_key_exists($name, $this->_subForms)) {
1718            unset($this->_subForms[$name]);
1719            if (array_key_exists($name, $this->_order)) {
1720                unset($this->_order[$name]);
1721                $this->_orderUpdated = true;
1722            }
1723            return true;
1724        }
1725
1726        return false;
1727    }
1728
1729    /**
1730     * Remove all form subForms/subforms
1731     *
1732     * @return Zend_Form
1733     */
1734    public function clearSubForms()
1735    {
1736        foreach (array_keys($this->_subForms) as $key) {
1737            if (array_key_exists($key, $this->_order)) {
1738                unset($this->_order[$key]);
1739            }
1740        }
1741        $this->_subForms     = array();
1742        $this->_orderUpdated = true;
1743        return $this;
1744    }
1745
1746
1747    // Display groups:
1748
1749    /**
1750     * Set default display group class
1751     *
1752     * @param  string $class
1753     * @return Zend_Form
1754     */
1755    public function setDefaultDisplayGroupClass($class)
1756    {
1757        $this->_defaultDisplayGroupClass = (string) $class;
1758        return $this;
1759    }
1760
1761    /**
1762     * Retrieve default display group class
1763     *
1764     * @return string
1765     */
1766    public function getDefaultDisplayGroupClass()
1767    {
1768        return $this->_defaultDisplayGroupClass;
1769    }
1770
1771    /**
1772     * Add a display group
1773     *
1774     * Groups named elements for display purposes.
1775     *
1776     * If a referenced element does not yet exist in the form, it is omitted.
1777     *
1778     * @param  array $elements
1779     * @param  string $name
1780     * @param  array|Zend_Config $options
1781     * @return Zend_Form
1782     * @throws Zend_Form_Exception if no valid elements provided
1783     */
1784    public function addDisplayGroup(array $elements, $name, $options = null)
1785    {
1786        $group = array();
1787        foreach ($elements as $element) {
1788            if($element instanceof Zend_Form_Element) {
1789                $elementName = $element->getName();
1790                if (!isset($this->_elements[$elementName])) {
1791                    $this->addElement($element);
1792                }
1793                $element = $elementName;
1794            }
1795
1796            if (isset($this->_elements[$element])) {
1797                $add = $this->getElement($element);
1798                if (null !== $add) {
1799                    $group[] = $add;
1800                }
1801            }
1802        }
1803        if (empty($group)) {
1804            require_once 'Zend/Form/Exception.php';
1805            throw new Zend_Form_Exception('No valid elements specified for display group');
1806        }
1807
1808        $name = (string) $name;
1809
1810        if (is_array($options)) {
1811            $options['form']     = $this;
1812            $options['elements'] = $group;
1813        } elseif ($options instanceof Zend_Config) {
1814            $options = $options->toArray();
1815            $options['form']     = $this;
1816            $options['elements'] = $group;
1817        } else {
1818            $options = array(
1819                'form'     => $this,
1820                'elements' => $group,
1821            );
1822        }
1823
1824        if (isset($options['displayGroupClass'])) {
1825            $class = $options['displayGroupClass'];
1826            unset($options['displayGroupClass']);
1827        } else {
1828            $class = $this->getDefaultDisplayGroupClass();
1829        }
1830
1831        if (!class_exists($class)) {
1832            require_once 'Zend/Loader.php';
1833            Zend_Loader::loadClass($class);
1834        }
1835        $this->_displayGroups[$name] = new $class(
1836            $name,
1837            $this->getPluginLoader(self::DECORATOR),
1838            $options
1839        );
1840
1841        if (!empty($this->_displayGroupPrefixPaths)) {
1842            $this->_di…

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