Add custom field in the view of yii2 which is not in model

帅比萌擦擦* 提交于 2019-12-10 21:45:17

问题


I am getting a problem while saving a form in yii2.

I have created a custom field with name like other fields Myposts['categoryLevel2']. This field is not in model. It is a conditional field. When I post my from I assign its value to a model attribute Like:

$categoryLevel3 = $request->post('categoryLevel3');
if(!empty($categoryLevel3)){
    $model->category=$categoryLevel3;
}

Now because categoryLevel3 is not there in table post so it is giving error. Getting unknown property: frontend\models\Posts I know the issue. The error is because in $_POST array there is a field categoryLevel3 now and it is not in table so $model->save() is throwing exception. I tried unset($_POST['categoryLevel3')) but that also did not work. Can anyone help me on this?

How can I create a filed in view which is not in table and ignore it before $model->save?


回答1:


Add Public property 'categoryLevel3 in Your Post Model Class.

 class Post extends yii\db\ActiveRecord{
   public $categoryLevel3;

   public function rules(){

       return [

          [''categoryLevel3' , 'required']
   ...
           ];

     }
   }


来源:https://stackoverflow.com/questions/33970348/add-custom-field-in-the-view-of-yii2-which-is-not-in-model

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!