3
* CFormElementCollection class file.
5
* @author Qiang Xue <qiang.xue@gmail.com>
6
* @link http://www.yiiframework.com/
7
* @copyright Copyright © 2008-2011 Yii Software LLC
8
* @license http://www.yiiframework.com/license/
12
* CFormElementCollection implements the collection for storing form elements.
14
* Because CFormElementCollection extends from {@link CMap}, it can be used like an associative array.
17
* $element=$collection['username'];
18
* $collection['username']=array('type'=>'text', 'maxlength'=>128);
19
* $collection['password']=new CFormInputElement(array('type'=>'password'),$form);
20
* $collection[]='some string';
23
* CFormElementCollection can store three types of value: a configuration array, a {@link CFormElement}
24
* object, or a string, as shown in the above example. Internally, these values will be converted
25
* to {@link CFormElement} objects.
27
* @author Qiang Xue <qiang.xue@gmail.com>
28
* @version $Id: CFormElementCollection.php 2799 2011-01-01 19:31:13Z qiang.xue $
29
* @package system.web.form
32
class CFormElementCollection extends CMap
39
* @param CForm $form the form object that owns this collection
40
* @param boolean $forButtons whether this collection is used to store buttons.
42
public function __construct($form,$forButtons=false)
44
parent::__construct();
46
$this->_forButtons=$forButtons;
50
* Adds an item to the collection.
51
* This method overrides the parent implementation to ensure
52
* only configuration arrays, strings, or {@link CFormElement} objects
53
* can be stored in this collection.
54
* @param mixed $key key
55
* @param mixed $value value
56
* @throws CException if the value is invalid.
58
public function add($key,$value)
65
if($this->_forButtons)
67
$class=$this->_form->buttonElementClass;
68
$element=new $class($value,$this->_form);
72
if(!isset($value['type']))
73
$value['type']='text';
74
if($value['type']==='string')
76
unset($value['type'],$value['name']);
77
$element=new CFormStringElement($value,$this->_form);
79
else if(!strcasecmp(substr($value['type'],-4),'form')) // a form
81
$class=$value['type']==='form' ? get_class($this->_form) : Yii::import($value['type']);
82
$element=new $class($value,null,$this->_form);
86
$class=$this->_form->inputElementClass;
87
$element=new $class($value,$this->_form);
91
else if($value instanceof CFormElement)
93
if(property_exists($value,'name') && is_string($key))
98
$element=new CFormStringElement(array('content'=>$value),$this->_form);
99
parent::add($key,$element);
100
$this->_form->addedElement($key,$element,$this->_forButtons);
104
* Removes the specified element by key.
105
* @param string $key the name of the element to be removed from the collection
107
public function remove($key)
109
if(($item=parent::remove($key))!==null)
110
$this->_form->removedElement($key,$item,$this->_forButtons);