开发者

yii CFormModel dynamic properties

i got such form

    class CC extends CFormModel
{
    public $static_field;
    public $fields;

    public function rules()
    {
        return array(
            array('static_field, testF', 'required')
        );
    }



    public function getForm()
    {
    开发者_如何学C    return new CForm(array(
            'showErrorSummary'=>true,
            'elements'=>array(
                'static_field'=>array(),
                'testF'=>array(),
            ),
            'buttons'=>array(
                'submit'=>array(
                    'type'=>'submit',
                    'label'=>'Next'
                )
            )
        ), $this);
    }

    public function attributeLabels()
    {
        return array(
            'static_field' => 'static_field'
        );
    }


    public function  __get($name)
    {
        if (isset($this->fields[$name]))
            return $this->fields[$name];
        else
            return '';
    }

    public function  __set($name, $value)
    {
        $this->fields[$name] = $value;
    }
}

i want to add dynamical field testF

i try to use __get\__set and array for values, but nothing work. any ideas?


If by dynamic you mean not required, you can add it as a property just as you have done with static_field. All attributes, or fields, are encapsulated member data of your FormModel class. So, if you wanted to add your dynamic_field attribute, you could add it in this manner:

class CC extends CFormModel
{
    public $static_field;
    public $dynamic_field;

    public function rules()
    {
        return array(
            array('static_field','required'),
            array('dynamic_field','safe'),
        );
    }
}

Also, you're not exactly following the dominant usage pattern for this type of class. If I were you, I would suggest creating some CRUD through gii and examining the usage patterns for models and forms.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜