I am getting error; Is there any complete example of how to use the behavior(configuration in model,view,controller) practically

Is there any complete example of how to use the behavior(configuration in model,view,controller) and what are requirements to use the extension(which libraries are need). I am confused I don’t know what to do things that I am trying are not working:

In my model:


public $photo;//The file attributes

public function behaviors(){

return [

[

"class" => "sjaakp\illustrated\Illustrated",

"attributes"=>[

'img'=>[

'aspectRatio'=>1.0,

'cropSize'=>90,]

]

],

];

}

//rules : $rules[] = ['photo','file'];

In my view


use sjaakp\illustrated\Uploader;


    <div id="partiechargee" class="panel panel-default center-block">

        <?php if($titre==true){

         echo '<p class="well-sm">';

       $this->render('/_alert', ['module' => $module,]);

        echo '</p>';}?>


        <h3 class="panel-title panel-heading text-center" style="margin-bottom:5px;"><strong><?= Html::encode($this->title)?></strong></h3>

            <?php $form = ActiveForm::begin([

                 'id'=> 'form-registration',

                'enableAjaxValidation'   => true,

                'enableClientValidation' => false,

                'method'=>'post',

                'action'=>\Yii::$app->urlManager->createUrl(['settings/profilephoto']),

            'options'=>['class'=>'well-sm text-center',

                'enctype'=>'multipart/form-data']]); ?>


<?= $form->field($model, 'photo')->widget(Uploader::className([

'deleteOptions' => [

'label' => '', // Font Awesome icon

'title' => 'Delete image'

]

])) ?>




<?= Html::submitButton(Yii::t('app', 'Exit'), ['class' => 'btn btn-danger cancel btn-block']) ?>


<?php ActiveForm::end(); ?>



With that I am getting errror : Undefined index: photo

Add ‘photo’ to your model, then.

It is there check the code I posted model has an attribute photo.

I saw that. Sorry :)

Anyway… Why don’t you debug your code to see what’s really in $model?

Thanks; I went through the extension files to understand how it works and then I got how to correct it. In the model the behavior must be configure as follow:

Incorrect Configuration:


public function behaviors(){

return [

[

"class" => "sjaakp\illustrated\Illustrated",

"attributes"=>[

'img'=>[

'aspectRatio'=>1.0,

'cropSize'=>90,]

]

],

];

}

Correct Configuration:


public function behaviors(){

return [

[

"class" => "sjaakp\illustrated\Illustrated",

"attributes"=>[

'photo'//Important to put the attribute of fileinput,

'img'=>[

'aspectRatio'=>1.0,

'cropSize'=>90,]

]

],

];

}

So it is being rendered but the when I click the submit button it is sending an ajax request to the server but it not saving the file here is my code in the controller(what am I missing in the action so that it saves the image) :


public function actionphoto(){

     $model = Profile::findOne(Yii::$app->user->identity->getId());

     $model->scenario='photo';

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

        var_dump('Image Saved');

     }

  

          if(\Yii::$app->request->isAjax){

              var_dump('ajax request');

              \yii::$app->end();

              return $this->renderAjax('photo',array('model'=>$model));

          }else{

              return $this->render('photo',array('model'=>$model));

          }

    } 

You are almost there, but you need to actually save the image. :)

See this:

http://code.tutsplus…ails–cms-23196

http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html

Ok; I thought the behaviour was going to save automatically after calling the save method of the model since I set ‘directory’=>’@webroot’,‘illustrationDirectory’=>‘media’ when configuring the behavior.

I read My link yesterday when you suggested it to me while looking for altenartives but I tried and did’nt go for it since the kartik-extension is not working properly for me after so many acrobatics tried(problem post in My link).

I runned to illustrated after the failure making kartik to work.

I put;

In controller:




public function actionphoto(){

     $model = Profile::findOne(Yii::$app->user->identity->getId());

     $model->scenario='photo';

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

         $model->photo = UploadedFile::getInstances($model, 'photo');

            if ($model->upload()) {

                var_dump('Image Enregistrer');

            }

     }

          if(\Yii::$app->request->isAjax){

              var_dump('ajax request');

              \yii::$app->end();

              return $this->renderAjax('photo',array('model'=>$model));

          }else{

              return $this->render('photo',array('model'=>$model));

          }

    } 

 



In model :


 public function upload()

    {

         

        if ($this->validate()) {

            $this->photo->saveAs('uploads/' . $this->photo->baseName . '.' . $this->photo->extension);

            return true;

        } else {

            var_dump('NOT VALID');

        }



When I click on the submit button I get this:

string ‘NOT VALID’ (length=9)

string ‘ajax request’ (length=12)

as response

And if I remove


$this->validate()

condition I receive “PHP Notice ‘yii\base\ErrorException’ with message ‘Trying to get property of non-object’” error message on the line


$this->photo->saveAs('uploads/' . $this->photo->baseName . '.' . $this->photo->extension);

(because of using $this->photo->saveAs,$this->photo->baseNamen,$this->photo->extension)

So that is the problem now!!!