Yii $form->textfield, how to set a default value?

后端 未结 7 785
孤独总比滥情好
孤独总比滥情好 2021-02-12 10:55

So I am fiddling with the Yii Framework and in one of the views, specifically the create form, I am trying to give one of my textfields a default value. Therefore when I go onto

7条回答
  •  悲&欢浪女
    2021-02-12 11:54

    Always, is a good idea deal with data (defaul values, change after something happening, data treatment, etc) on the model class.

    If you're getting the value from after initialize the model, the best way is to use the method init().

    But, if you want to change, or define a default value after load data from the database, you can use the method afterFind()

    For example:

    public function afterFind(){
        $this->localdate = date("Y-m-d");
        parent::afterFind();
    }
    

    This link has a lot of usefull information about these methods: http://www.yiiframework.com/doc/guide/1.1/en/database.ar#customization

提交回复
热议问题