Yii2 - Kartik file input - update


(Folumikeidowu) #1

I have successfully save files such as pdf and docx using kartik file input.

Controller: actionCreate




    public function actionCreate()

    {    

$model = new Courses(); 

        if ($model->load(Yii::$app->request->post())) {

		$model->attributes = $_POST['Courses'];

                $model->course_format = 'Topic';

                $model->is_structure =1;

		$model->course_start_date = Yii::$app->dateformatter->getDateFormat($_POST['Courses']['course_start_date']);

		$model->course_end_date = Yii::$app->dateformatter->getDateFormat($_POST['Courses']['course_end_date']);

		$model->updated_by = Yii::$app->getid->getId();

		$model->updated_at= new \yii\db\Expression('NOW()');

                

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

        if(!is_null($coursefile))

        {

          $model->course_file_name = $coursefile->name;

          

          $ext = end((explode(".", $coursefile->name)));

          // generate a unique file name to prevent duplicate filenames

          $model->course_file_path = Yii::$app->security->generateRandomString().".{$ext}";

          // the path to save file, you can set an uploadPath

          Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/data/course_files/course_outlines/';

          $path = Yii::$app->params['uploadPath'] . $model->course_file_path;

          $coursefile->saveAs($path);

  //    }         //validate 

        }                 

                $model->save(false);


                        return $this->redirect(['available-courses-formats']);

        } else {

            return $this->render('course-structure-create', [

                'model' => $model,

            ]);

        }        

    }



model




    public function rules()

    {

        return [

            [['course_code', 'course_name', 'course_faculty_id', 'course_department_id', 'course_programme_id'], 'required', 'message' => ''],

            [['course_academic_year_id', 'course_academic_semester_id', 'course_faculty_id', 'course_department_id', 'course_programme_id', 'course_unit','sort_order', 'course_format_no', 'maxbytes', 'legacy_files', 'show_reports', 'visible', 'group_mode', 'group_mode_force', 'default_grouping_id', 'requested', 'restrict_modules', 'enable_completion', 'completion_starton_enrol', 'completion_notify', 'is_status', 'created_by', 'updated_by', 'is_structure'], 'integer', 'message' => ''],

            [['course_summary'], 'string'],

            [['course_start_date', 'course_end_date', 'created_at', 'updated_at'], 'safe'],

            [['course_type'], 'string', 'max' => 20],

            [['course_name'], 'string', 'max' => 254],

            [['course_code'], 'string', 'max' => 50],

            [['course_num', 'hidden_sections'], 'string', 'max' => 100],

            [['course_format'], 'string', 'max' => 10],

            [['course_file_path', 'course_file_name'], 'string', 'max' => 300],

            [['course_code', 'course_academic_year_id'], 'unique', 'targetAttribute' => ['course_code', 'course_academic_year_id'], 'message' => yii::t('course', 'Academic Year already exist for this Course Code.'), 'when' => function ($model){ return $model->is_status == 0;}],

            [['coursefile'], 'safe'],

            [['course_name', 'course_academic_year_id'], 'unique', 'targetAttribute' => ['course_name', 'course_academic_year_id'], 'message' => yii::t('course', 'Academic Year already exist for this Course.'), 'when' => function ($model){ return $model->is_status == 0;}],

            [['coursefile'], 'file', 'skipOnEmpty' => true, 'extensions' => 'pdf, xls, xlsx, doc, docx'],   // 'maxFiles' => 1,, 'skipOnEmpty' => true

            [['coursefile'], 'file', 'maxSize'=>'10000000'] 

        ];

    }



I have this questions

  1. How do I re-write my actionUpdate controller to perform Yii2 Kartik file input update

See the controller below:




    public function actionUpdate($id)

    {

        $model = $this->findModel($id);


        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->id]);

        } else {

            return $this->render('update', [

                'model' => $model,

            ]);

        }

    }



  1. How do I download the uploaded documents

  2. How do I write a code from the controller to make the attributes (course_start_date and course_end_date) to be required. I don’t want to do it from the model