Permalink
Browse files

refactoring form feature.

  • Loading branch information...
1 parent 27cc745 commit 0044126b2adb4b66b44f3559192afeb9e1810540 qiang.xue committed Jun 26, 2009
View
@@ -631,6 +631,12 @@ public static function t($category,$message,$params=array(),$source=null,$langua
'CFilter' => '/web/filters/CFilter.php',
'CFilterChain' => '/web/filters/CFilterChain.php',
'CInlineFilter' => '/web/filters/CInlineFilter.php',
+ 'CForm' => '/web/form/CForm.php',
+ 'CFormButtonElement' => '/web/form/CFormButtonElement.php',
+ 'CFormElement' => '/web/form/CFormElement.php',
+ 'CFormElementCollection' => '/web/form/CFormElementCollection.php',
+ 'CFormInputElement' => '/web/form/CFormInputElement.php',
+ 'CFormStringElement' => '/web/form/CFormStringElement.php',
'CGoogleApi' => '/web/helpers/CGoogleApi.php',
'CHtml' => '/web/helpers/CHtml.php',
'CJSON' => '/web/helpers/CJSON.php',
View
@@ -262,6 +262,18 @@ public function isAttributeRequired($attribute)
return false;
}
+ /**
+ * Returns a value indicating whether the attribute is safe for massive assignments.
+ * @param string attribute name
+ * @return boolean whether the attribute is safe for massive assignments
+ * @since 1.1
+ */
+ public function isAttributeSafe($attribute)
+ {
+ $attributes=$this->getSafeAttributeNames();
+ return in_array($attribute,$attributes);
+ }
+
/**
* Returns the text label for the specified attribute.
* @param string the attribute name
@@ -129,7 +129,12 @@ public function itemAt($key)
public function add($key,$value)
{
if(!$this->_r)
- $this->_d[$key]=$value;
+ {
+ if($key===null)
+ $this->_d[]=$value;
+ else
+ $this->_d[$key]=$value;
+ }
else
throw new CException(Yii::t('yii','The map is read only.'));
}
@@ -9,9 +9,9 @@
*/
/**
- * CForm is the base class providing the common features needed by data model objects.
+ * CForm represents a form object that contains form input specification.
+ *
*
- * CForm defines the basic framework for data models that need to be validated.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
@@ -37,7 +37,7 @@ class CForm extends CFormElement implements ArrayAccess
public function __construct($parent,$model=null)
{
- $this->parent=$parent;
+ parent::__construct($parent);
$this->_model=$model;
}
@@ -49,25 +49,12 @@ public function submitted($loadData=true)
return $ret;
}
- protected function getUniqueId()
- {
- if(isset($this->attributes['id']))
- return 'yform_'.$this->attributes['id'];
- else
- return 'yform_'.sprintf('%x',crc32(serialize(array_keys($this->getElements()->toArray()))));
- }
-
- public function getSubmitMethod()
- {
- return strcasecmp($this->getRoot()->method,'get') ? 'post' : 'get';
- }
-
public function loadData()
{
if($this->_model!==null)
{
$class=get_class($this->_model);
- if($this->getSubmitMethod()==='get')
+ if($this->getRoot()->method==='get')
{
if(isset($_GET[$class]))
$this->_model->setAttributes($_GET[$class]);
@@ -84,41 +71,33 @@ public function loadData()
public function clicked($name)
{
- if($this->getSubmitMethod()==='get')
+ if($this->getRoot()->method==='get')
return isset($_GET[$name]);
else
return isset($_POST[$name]);
}
- protected function evaluateVisible()
- {
- foreach($this->getElements() as $element)
- if($element->getVisible())
- return true;
- return false;
- }
-
public function getRoot()
{
$root=$this;
- while($root->parent instanceof self)
- $root=$root->parent;
+ while($root->getParent() instanceof self)
+ $root=$root->getParent();
return $root;
}
public function getOwner()
{
- $owner=$this->parent;
+ $owner=$this->getParent();
while($owner instanceof self)
- $owner=$owner->parent;
+ $owner=$owner->getParent();
return $owner;
}
public function getModel()
{
$form=$this;
- while($form->_model===null && $form->parent instanceof self)
- $form=$form->parent;
+ while($form->_model===null && $form->getParent() instanceof self)
+ $form=$form->getParent();
return $form->_model;
}
@@ -157,30 +136,23 @@ public function setButtons($buttons)
public function render()
{
- $output='';
- if(!$this->parent instanceof self)
- $output.=$this->begin();
- $output.=$this->body();
- if(!$this->parent instanceof self)
- $output.=$this->end();
- return $output;
+ if($this->getParent() instanceof self) // is a sub-form
+ return $this->body();
+ else
+ return $this->begin() . $this->body() . $this->end();
}
public function token()
{
- if(isset($this->attributes['name']))
- $name=$this->attributes['name'];
- else
- $name='yform';
- return CHtml::hiddenField($this->getUniqueID(),1);
+ return '<div style="visibility:none">'.CHtml::hiddenField($this->getUniqueID(),1).'</div>';
}
public function begin()
{
$output=CHtml::beginForm($this->action,$this->method,$this->attributes);
+ $output.=$this->token();
if($this->legend!==null)
$output.="\n<fieldset>\n<legend>\n".$this->legend."</legend>\n";
- $output.='<div style="visibility:none">'.$this->token().'</div>';
return $output;
}
@@ -218,6 +190,32 @@ public function body()
return $output;
}
+ /**
+ * Evaluates the visibility of this form.
+ * This method will check the visibility of the {@link elements}.
+ * If any one of them is visible, the form is considered as visible. Otherwise, it is invisible.
+ * @return boolean whether this form is visible.
+ */
+ protected function evaluateVisible()
+ {
+ foreach($this->getElements() as $element)
+ if($element->getVisible())
+ return true;
+ return false;
+ }
+
+ /**
+ * Returns a unique ID that identifies this form in the current page.
+ * @return string the unique ID identifying this form
+ */
+ protected function getUniqueId()
+ {
+ if(isset($this->attributes['id']))
+ return 'yform_'.$this->attributes['id'];
+ else
+ return 'yform_'.sprintf('%x',crc32(serialize(array_keys($this->getElements()->toArray()))));
+ }
+
/**
* Returns whether there is an element at the specified offset.
* This method is required by the interface ArrayAccess.
@@ -261,5 +259,3 @@ public function offsetUnset($offset)
$this->getElements()->remove($offset);
}
}
-
-
@@ -61,17 +61,6 @@ class CFormButtonElement extends CFormElement
private $_on;
- /**
- * Evaluates the visibility of this element.
- * This method will check the {@link on} property to see if
- * the model is in a scenario that should have this string displayed.
- * @return boolean whether this element is visible.
- */
- protected function evaluateVisible()
- {
- return empty($this->_on) || in_array($this->getParent()->getModel()->getScenario(),$this->_on);
- }
-
/**
* Returns a value indicating under which scenarios this button is visible.
* If the value is empty, it means the button is visible under all scenarios.
@@ -123,4 +112,15 @@ public function render()
return ob_get_clean();
}
}
+
+ /**
+ * Evaluates the visibility of this element.
+ * This method will check the {@link on} property to see if
+ * the model is in a scenario that should have this string displayed.
+ * @return boolean whether this element is visible.
+ */
+ protected function evaluateVisible()
+ {
+ return empty($this->_on) || in_array($this->getParent()->getModel()->getScenario(),$this->_on);
+ }
}
@@ -24,12 +24,8 @@
* @var array list of attributes (name=>value) for the HTML element represented by this object.
*/
public $attributes=array();
- /**
- * @var mixed the direct parent of this element. This could be either a {@link CForm} object or a {@link CBaseController} object
- * (a controller or a widget).
- */
- public $parent;
+ private $_parent;
private $_visible;
/**
@@ -46,7 +42,7 @@
*/
public function __construct($parent,$config=null)
{
- $this->parent=$parent;
+ $this->_parent=$parent;
$this->configure($config);
}
@@ -146,6 +142,15 @@ public function setVisible($value)
$this->_visible=$value;
}
+ /**
+ * @return mixed the direct parent of this element. This could be either a {@link CForm} object or a {@link CBaseController} object
+ * (a controller or a widget).
+ */
+ public function getParent()
+ {
+ return $this->_parent;
+ }
+
/**
* Evaluates the visibility of this element.
* Child classes should override this method to implement the actual algorithm
@@ -43,7 +43,7 @@ public function __construct($form,$forButtons=false)
{
parent::__construct();
$this->_form=$form;
- $this->_forButtons=$buttonOnly;
+ $this->_forButtons=$forButtons;
}
/**
@@ -79,17 +79,6 @@ class CFormInputElement extends CFormElement
private $_label;
- /**
- * Evaluates the visibility of this element.
- * This method will check the {@link on} property to see if
- * the model is in a scenario that should have this string displayed.
- * @return boolean whether this element is visible.
- */
- protected function evaluateVisible()
- {
- return $this->getParent()->getModel()->isAttributeSafe($this->name);
- }
-
/**
* @return boolean whether this input is required. The value is determined by calling
* {@link CModel::isAttributeRequired} for the associated model and attribute of this input.
@@ -140,7 +129,7 @@ public function render()
*/
public function renderLabel()
{
- return CHtml::activeLabelEx($this->getForm()->getModel(), $this->name, array('label'=>$this->getLabel()));
+ return CHtml::activeLabelEx($this->getParent()->getModel(), $this->name, array('label'=>$this->getLabel()));
}
/**
@@ -154,14 +143,14 @@ public function renderInput()
{
$method=self::$coreTypes[$this->type];
if(strpos($method,'List')!==false)
- return CHtml::$method($this->getForm()->getModel(), $this->name, $this->items, $this->attributes);
+ return CHtml::$method($this->getParent()->getModel(), $this->name, $this->items, $this->attributes);
else
- return CHtml::$method($this->getForm()->getModel(), $this->name, $this->attributes);
+ return CHtml::$method($this->getParent()->getModel(), $this->name, $this->attributes);
}
else
{
$attributes=$this->attributes;
- $attributes['model']=$this->getForm()->getModel();
+ $attributes['model']=$this->getParent()->getModel();
$attributes['attribute']=$this->name;
ob_start();
$this->getParent()->getOwner()->widget($this->type, $attributes);
@@ -176,7 +165,7 @@ public function renderInput()
*/
public function renderError()
{
- return CHtml::error($this->getForm()->getModel(), $this->name);
+ return CHtml::error($this->getParent()->getModel(), $this->name);
}
/**
@@ -188,4 +177,15 @@ public function renderHint()
{
return $this->hint===null ? '' : '<p class="hint">'.$this->hint.'</p>';
}
+
+ /**
+ * Evaluates the visibility of this element.
+ * This method will check the {@link on} property to see if
+ * the model is in a scenario that should have this string displayed.
+ * @return boolean whether this element is visible.
+ */
+ protected function evaluateVisible()
+ {
+ return $this->getParent()->getModel()->isAttributeSafe($this->name);
+ }
}
@@ -64,6 +64,6 @@ public function render()
*/
protected function evaluateVisible()
{
- return empty($this->_on) || in_array($this->getForm()->getModel()->getScenario(),$this->_on);
+ return empty($this->_on) || in_array($this->getParent()->getModel()->getScenario(),$this->_on);
}
}
Oops, something went wrong.

0 comments on commit 0044126

Please sign in to comment.