şöyle birşey buldum -) ama küçük bir hata var
<?php
/*
* Dynamic Form
*/
class Element {
public $_data = array();
public function __construct($_data = array()) {
$this->_data = $_data;
}
public function __get($name) {
if (array_key_exists($name, $this->_data)) {
return $this->_data[$name];
}
return null;
}
public function __toString() {
return $this->_data["name"];
}
}
class DynamicForm extends CFormModel {
public $elements = array();
public function rules() {
$required = array();
foreach ($this->elements as $e)
if ($e->required)
$required[] = $e->name;
return array(
array(implode(', ', $required), 'required'),
);
}
public function __get($name) {
foreach($this->elements as $e)
if($e->name == $name) return $e->value;
return $this->$name;
}
public function attributeLabels() {
return $attributeLabels = array();
}
}
?>
Ör
$form = new DynamicForm;
$form->elements = array(
//general
new Element(array('tab' => 'general', 'name' => 'name', 'required' => true, 'type' => 'textfield')),
new Element(array('tab' => 'general', 'name' => 'url', 'required' => true, 'type' => 'textfield')),
new Element(array('tab' => 'general', 'name' => 'owner', 'required' => true, 'type' => 'textfield')),
);
//
//view
echo CHtml::beginForm(array('/admin/store/DynamicForm'), 'post', array('id' => 'store-setting-form'));
foreach ($model->elements as $e) {
if ($e->tab == $tab) {
echo '<div class="row">' . "\n";
echo CHtml::activeLabelEx($model, $e->name);
switch ($e->type) {
case 'textfield':
echo CHtml::activeTextField($model, $e->name);
break;
case 'dropdown':
echo CHtml::activeDropDownList($model, $e->name, $e->data, array('prompt' => __('stoe', 'Choose Item')));
break;
case 'textarea':
echo CHtml::activeTextArea($model, $e->name, array('rows' => 6, 'cols' => 50));
break;
case 'radiobuttonlist':
echo '<div class="itemlist">';
echo CHtml::activeRadioButtonList($model, $e->name, $e->data, array('separator' => ' '));
echo '</div>' . "\n\n";
break;
case 'checkboxlist':
echo ' <div class="itemlist">';
echo CHtml::activeCheckBoxList($model, $e->name, $e->data, array('separator' => ' '));
echo '</div>' . "\n\n";
break;
}
echo '</div>' . "\n\n";
}
}
echo CHtml::endForm();
Şu hatayı alıyorum
[html]
Undefined property: DynamicForm::$attributes
Source File
D:\PROJELER\yiishop.com\public_html\protected\modules\admin\models\DynamicForm.php(49)
00037: if ($e->required)
00038: $required[] = $e->name;
00039:
00040: return array(
00041: array(implode(’, ', $required), ‘required’),
00042: );
00043: }
00044:
00045: public function __get($name) {
00046: foreach($this->elements as $e)
00047: if($e->name == $name) return $e->value;
00048:
00049: return $this->$name;
00050: }
00051:
00052: public function attributeLabels() {
00053: return $attributeLabels = array();
00054: }
00055:
00056: }
00057:
00058: ?>
[/html]