Yii Framework Forum: dinamik form model - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

dinamik form model

#1 User is offline   Cihan Ozturk 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 02-June 10
  • Location:İstanbul

Posted 25 December 2010 - 12:57 PM

Merhaba

dinamik şekilde form oluşturmak istiyorum bu formdaki fieldlar ve tipleri bir dizi de olacak

form kayıt edildiğinde form daki verileri option tablosuna

key value stunlarına kayıt etmek isitiyorum ve tabi güncelleme de yapıcak



örnek
array(
  'title'=>array('type'=>'text','minlenght'=>20,required=true)
  'name'=>array('type'=>'text','minlenght'=>20,required=true)
)


tabloya kayıt ederken

key value
----------------
title cihan
name test

gibi olacak

teşşekürler
0

#2 User is offline   Cihan Ozturk 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 02-June 10
  • Location:İstanbul

Posted 27 December 2010 - 08:49 AM

şö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

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: ?>



0

#3 User is offline   Umut Gundogdu 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 27-November 08
  • Location:Ankara

Posted 03 January 2011 - 05:43 PM

Biraz gec oldu ama

$this->$name DynamicForm da tanimli degil $this->elements->$name veya $this->elements[$name] kullansan istedigin olacak gibi
0

#4 User is offline   ahmet 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 15-September 10
  • Location:Nicosia

Posted 23 March 2011 - 08:26 AM

yii-user eklentisinde profile alanları için dediğin kullanım var. Yardımı olabilir. http://code.google.c...els/Profile.php
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users