-2

I'm new in yii2... I need to use this widget in yii2 app: https://github.com/2amigos/yii2-file-upload-widget

But when I install it, throw this error: Undefined variable: model

There is a screen: http://prntscr.com/bnvul8

Any suggession?

-- controllers/AdminController.php -- (when is pass the $model, is undefined in here... but, on the github page, don't use this pass.)

public function actionGallery()

{
    if (Yii::$app->user->isGuest) {
        return $this->redirect('index');
    }

    $this->layout = 'adminlayout';

    return $this->render('gallery', [
        'model'=> $model
    ]);
}

-- view/gallery.php --

use dosamigos\fileupload\FileUploadUI;

...

<?= FileUploadUI::widget([
'model' => $model,
'attribute' => 'image',
'url' => ['media/upload', 'id' => $tour_id],
'gallery' => false,
'fieldOptions' => [
    'accept' => 'image/*'
],
'clientOptions' => [
    'maxFileSize' => 2000000
],
// ...
'clientEvents' => [
    'fileuploaddone' => 'function(e, data) {
                                console.log(e);
                                console.log(data);
                            }',
    'fileuploadfail' => 'function(e, data) {
                                console.log(e);
                                console.log(data);
                            }',
],

1 Answers1

0

I have a suggestion:

You have to create the variable $model before you can send it to your view. A model is a template for a database connection or a form, and you have to have that connected to upload with this widget.

That being said, you should start by learning the basics of the framework, you should not start by jumping into extensions and file uploading.

Have a look here: Working with forms

Jørgen
  • 2,827
  • 5
  • 26
  • 45
  • Ok, I will try, but the github page don't mention about to create the model, before use the plugin... – Péter Zajácz Jul 02 '16 at 14:36
  • That's why i added the comment about learning the basics. If you know the basics of Yii2 development, you understand from the context that there should be a model, and an attribute in that model, to handle the file uploads. Good luck. – Jørgen Jul 02 '16 at 14:47
  • Pfff... So, i try, but don't know, how to bulid a model file for this plugin. Can u show me an example or something that might help? – Péter Zajácz Jul 02 '16 at 17:17
  • Create a database table with at least `id` (PK) and `image` (varchar) fields. Then navigate to your site and append /gii or ?r=gii and click on model generation, follow the steps and generate the model file. Then you have to make the `actionUpload()` (eksample in github repo). – Jørgen Jul 02 '16 at 17:23
  • And maybe check this link out http://webtips.krajee.com/ajax-based-file-uploads-using-fileinput-plugin/ . I recommend you follow some tutorials and get the hang of MVC and Yii2 in general. This is not the forum for step-by-step guidance. – Jørgen Jul 02 '16 at 17:25