duansha7453 2016-12-29 06:51
浏览 54

缺少id在urlindent传递

I am trying to upload file on server and to view the file uploaded I have to pass the model id to the view action. It is all good untill I add

'options' => ['enctype'=>'multipart/form-data']

to my ActiveForm. After I add it the id passed to the action view by URL doesn't show anymore and I get missing parameter message. i am using yii 2.0.2

Create action:

public function actionCreate()
{
    $model = new SubContent();

    if ($model->load(Yii::$app->request->post()) ) {
        //$fileName = $model->id;
        $model->save();
        $fileName = $model->id;

        $files =UploadedFile::getInstance($model,'files');

        $files->saveAs( 'uploads/'.$files->getBaseName().'.'.$files->getExtension());
        $model->files=$files;

        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

Form:

<div class="sub-content-form">

<?php $form = ActiveForm::begin(['options' => ['enctype'=>'multipart/form-data']]); ?>

    <?= $form->field($model, 'files')->fileInput(['maxlength' => 255]) ?>
    <?= $form->field($model, 'sub_id')
              ->dropDownList(ArrayHelper::map(Subjects::find()
              ->where(['year'=>\Yii::$app->user->identity->year])
              ->all(),'id','sub_name'),
              ['prompt'=>' select subject']
          ) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
<?php ActiveForm::end(); ?>

</div>

and this is my model

         <?php

       namespace frontend\models;

       use Yii;

            /**
        * This is the model class for table "sub_content".
           *
         * @property integer $id
        * @property string $files
           * @property integer $sub_id
           *
          * @property Subjects $sub
           */
            class SubContent extends \yii\db\ActiveRecord
           {
           /**
            * @inheritdoc
             */
             public static function tableName()
             {
            return 'sub_content';
               }

             public function rules()
            {
               return [
              [['files', 'sub_id'], 'required'],
             [['sub_id'], 'integer'],
           [['files'], 'string', 'max' => 255]


         ];
        }

        public function attributeLabels()
          {
           return [
           'id' => 'ID',
           'files' => 'Files',
            'sub_id' => 'Sub ID',
            ];
             }

           /**
          * @return \yii\db\ActiveQuery
           */
            public function getSub()
              {
               return $this->hasOne(Subjects::className(), ['id' => 
           'sub_id']);
           }
            }

edited..... still didnt work

  • 写回答

1条回答 默认 最新

  • doulu7174 2016-12-29 07:01
    关注

    First of all - since 2.0.8 you don't have to add enctype, it's done automatically when you add fileInput field.

    You are not verifying if $model->save() returned true and it looks like that model did not pass validation and because of that $model is not saved and because of that $model->id is not set.

    You should do something like:

    public function actionCreate()
    {
        $model = new SubContent();
    
        if ($model->load(Yii::$app->request->post()) ) {
    
            $model->files = UploadedFile::getInstance($model,'files');
    
            if ($model->save()) {
                $model->files->saveAs('uploads/' . $files->getBaseName() . '.' . $files->getExtension());
    
                return $this->redirect(['view', 'id' => $model->id]);
            }
        }
    
        return $this->render('create', ['model' => $model]);
    }
    

    For this to work you need to set proper validation rules in SubContent model.

    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂