3
* CFormElement 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
* CFormElement is the base class for presenting all kinds of form element.
14
* CFormElement implements the way to get and set arbitrary attributes.
16
* @author Qiang Xue <qiang.xue@gmail.com>
17
* @version $Id: CFormElement.php 2799 2011-01-01 19:31:13Z qiang.xue $
18
* @package system.web.form
21
abstract class CFormElement extends CComponent
24
* @var array list of attributes (name=>value) for the HTML element represented by this object.
26
public $attributes=array();
32
* Renders this element.
33
* @return string the rendering result
35
abstract function render();
39
* @param mixed $config the configuration for this element.
40
* @param mixed $parent the direct parent of this element.
43
public function __construct($config,$parent)
45
$this->configure($config);
46
$this->_parent=$parent;
50
* Converts the object to a string.
51
* This is a PHP magic method.
52
* The default implementation simply calls {@link render} and return
53
* the rendering result.
54
* @return string the string representation of this object.
56
public function __toString()
58
return $this->render();
62
* Returns a property value or an attribute value.
63
* Do not call this method. This is a PHP magic method that we override
64
* to allow using the following syntax to read a property or attribute:
66
* $value=$element->propertyName;
67
* $value=$element->attributeName;
69
* @param string $name the property or attribute name
70
* @return mixed the property or attribute value
71
* @throws CException if the property or attribute is not defined
74
public function __get($name)
77
if(method_exists($this,$getter))
78
return $this->$getter();
79
else if(isset($this->attributes[$name]))
80
return $this->attributes[$name];
82
throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.',
83
array('{class}'=>get_class($this), '{property}'=>$name)));
87
* Sets value of a property or attribute.
88
* Do not call this method. This is a PHP magic method that we override
89
* to allow using the following syntax to set a property or attribute.
91
* $this->propertyName=$value;
92
* $this->attributeName=$value;
94
* @param string $name the property or attribute name
95
* @param mixed $value the property or attribute value
98
public function __set($name,$value)
101
if(method_exists($this,$setter))
102
$this->$setter($value);
104
$this->attributes[$name]=$value;
108
* Configures this object with property initial values.
109
* @param mixed $config the configuration for this object. This can be an array
110
* representing the property names and their initial values.
111
* It can also be a string representing the file name of the PHP script
112
* that returns a configuration array.
114
public function configure($config)
116
if(is_string($config))
117
$config=require(Yii::getPathOfAlias($config).'.php');
118
if(is_array($config))
120
foreach($config as $name=>$value)
126
* Returns a value indicating whether this element is visible and should be rendered.
127
* This method will call {@link evaluateVisible} to determine the visibility of this element.
128
* @return boolean whether this element is visible and should be rendered.
130
public function getVisible()
132
if($this->_visible===null)
133
$this->_visible=$this->evaluateVisible();
134
return $this->_visible;
138
* @param boolean $value whether this element is visible and should be rendered.
140
public function setVisible($value)
142
$this->_visible=$value;
146
* @return mixed the direct parent of this element. This could be either a {@link CForm} object or a {@link CBaseController} object
147
* (a controller or a widget).
149
public function getParent()
151
return $this->_parent;
155
* Evaluates the visibility of this element.
156
* Child classes should override this method to implement the actual algorithm
157
* for determining the visibility.
158
* @return boolean whether this element is visible. Defaults to true.
160
protected function evaluateVisible()