Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 116 lines (110 sloc) 3.163 kB
27cc745
qiang.xue authored
1 <?php
2 /**
3 * CFormElementCollection class file.
4 *
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.yiiframework.com/
7 * @copyright Copyright &copy; 2008-2009 Yii Software LLC
8 * @license http://www.yiiframework.com/license/
9 */
10
11 /**
12 * CFormElementCollection implements the collection for storing form elements.
13 *
14 * Because CFormElementCollection extends from {@link CMap}, it can be used like an associative array.
15 * For example,
16 * <pre>
17 * $element=$collection['username'];
18 * $collection['username']=array('type'=>'text', 'maxlength'=>128);
048b958 Swapped the order of constructor parameters for CForm and related cla…
qiang.xue authored
19 * $collection['password']=new CFormInputElement(array('type'=>'password'),$form);
27cc745
qiang.xue authored
20 * $collection[]='some string';
21 * </pre>
22 *
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.
26 *
27 * @author Qiang Xue <qiang.xue@gmail.com>
28 * @version $Id$
29 * @package system.web.form
30 * @since 1.1
31 */
32 class CFormElementCollection extends CMap
33 {
34 private $_form;
35 private $_forButtons;
36
37 /**
38 * Constructor.
39 * @param CForm the form object that owns this collection
40 * @param boolean whether this collection is used to store buttons.
41 */
42 public function __construct($form,$forButtons=false)
43 {
44 parent::__construct();
45 $this->_form=$form;
0044126 refactoring form feature.
qiang.xue authored
46 $this->_forButtons=$forButtons;
27cc745
qiang.xue authored
47 }
48
49 /**
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
55 * @param mixed value
56 * @throws CException if the value is invalid.
57 */
58 public function add($key,$value)
59 {
60 if(is_array($value))
61 {
62e98fb
qiang.xue authored
62 if(is_string($key))
63 $value['name']=$key;
27cc745
qiang.xue authored
64
65 if($this->_forButtons)
66 {
67 $class=$this->_form->buttonElementClass;
048b958 Swapped the order of constructor parameters for CForm and related cla…
qiang.xue authored
68 $element=new $class($value,$this->_form);
27cc745
qiang.xue authored
69 }
70 else
71 {
72 if(!isset($value['type']))
73 $value['type']='text';
74 if($value['type']==='string')
75 {
76 unset($value['type'],$value['name']);
048b958 Swapped the order of constructor parameters for CForm and related cla…
qiang.xue authored
77 $element=new CFormStringElement($value,$this->_form);
27cc745
qiang.xue authored
78 }
62e98fb
qiang.xue authored
79 else if(!strcasecmp(substr($value['type'],-4),'form')) // a form
27cc745
qiang.xue authored
80 {
0ad7439
qiang.xue authored
81 $class=$value['type']==='form' ? get_class($this->_form) : Yii::import($value['type']);
048b958 Swapped the order of constructor parameters for CForm and related cla…
qiang.xue authored
82 $element=new $class($value,null,$this->_form);
27cc745
qiang.xue authored
83 }
84 else
62e98fb
qiang.xue authored
85 {
27cc745
qiang.xue authored
86 $class=$this->_form->inputElementClass;
048b958 Swapped the order of constructor parameters for CForm and related cla…
qiang.xue authored
87 $element=new $class($value,$this->_form);
62e98fb
qiang.xue authored
88 }
27cc745
qiang.xue authored
89 }
90 }
91 else if($value instanceof CFormElement)
92 {
62e98fb
qiang.xue authored
93 if(property_exists($value,'name') && is_string($key))
27cc745
qiang.xue authored
94 $value->name=$key;
b466b36 Refactored rendering about hidden fields.
qiang.xue authored
95 $element=$value;
27cc745
qiang.xue authored
96 }
97 else
048b958 Swapped the order of constructor parameters for CForm and related cla…
qiang.xue authored
98 $element=new CFormStringElement(array('content'=>$value),$this->_form);
b466b36 Refactored rendering about hidden fields.
qiang.xue authored
99 parent::add($key,$element);
100 $this->_form->addedElement($key,$element,$this->_forButtons);
101 }
102
e698c94 clean up of CForm.
qiang.xue authored
103 /**
104 * Removes the specified element by key.
105 * @param string the name of the element to be removed from the collection
106 */
b466b36 Refactored rendering about hidden fields.
qiang.xue authored
107 public function remove($key)
108 {
109 if($this->exists($key))
110 {
111 $item=parent::remove($key);
112 $this->_form->removedElement($key,$item,$this->_forButtons);
113 }
27cc745
qiang.xue authored
114 }
115 }
Something went wrong with that request. Please try again.