error loading an image in yii2

0

good afternoon my project has a client model, a clientController driver and a form guide me in the documentation of Yii2 but I do not give with the problem since it saves all the fields but I do not load the image in the route that I indicated , the REquest post is not generated.     

namespace app\models;

use Yii;



class Client extends \yii\db\ActiveRecord
{
/**
 * {@inheritdoc}
 */
 public $image;

public static function tableName()
{
    return 'client';
}

/**
 * {@inheritdoc}
 */
public function rules()
{
    return [
        [['name', 'country'], 'required'],
        [['name', 'country'], 'string', 'max' => 45],
    ];
}

/**
 * {@inheritdoc}
 */
public function attributeLabels()
{
    return [
        'idclient' => 'Codigo Cliente',
        'name' => 'Nombre',
        'country' => 'Pais',
        'image' => 'Imagen',
    ];
}
public function upload()
{
    if ($this->validate()) {
        $this->image->saveAs('uploads/' . $this->image->baseName . '.' . 
$this->image->extension);
        return true;
    } else {
        return false;
    }
}
}

my controller

 public function actionUpload(){
    $model = new Client();

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

        $model->image = UploadedFile::getInstance($model, 'image');

        if($model->validate()) {

            if ($model->upload() !== false) {
                $model->save();
            }

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

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

}

my Form

  <?php $form = ActiveForm::begin([
 "method" => "post",
 "enableClientValidation" => true,
 "options" => ["enctype" => "multipart/form-data"],
 ]);
 ?>

<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'country')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'image')->fileInput()?>

<div class="form-group">
    <?= Html::submitButton('Guardar', ['class' => 'btn btn-success']) ?>
</div>

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

the route

C: \ xampp \ htdocs \ basic \ uploads

POST request value

that is the request file

    
asked by Adrian Rojas 05.09.2018 в 22:23
source

2 answers

0

First of all, the field where the image path is saved is totally different from the image where it is loaded,

so the field you have in your model should be called differently

So you should change public $image; by public $imageFile; or some other name

Second, if you do not specify the field somewhere in the rules, it will not be saved.

If you do not want to set a specific rule just use safe

public function rules()
{
   return [
       [['name', 'country'], 'required'],
       [['name', 'country'], 'string', 'max' => 45],
       [['image', 'imageFile'], 'safe']
   ];
}

In the third you should check if there is a route in which you want to store, so

public function upload()
{
    if ($this->validate()) {
        FileHelper::createDirectory(Yii::getAlias('@app')."/uploads", 0777);
        $this->imageFile->saveAs(Yii::getAlias('@app')'/uploads/' .
        $this->imageFile->baseName . '.' .$this->imageFile->extension);
        $this->image = $this->imageFile->baseName . '.' .$this->imageFile->extension;
        return true;
    } else {
        return false;
    }
}

In case the alias @app does not exist, you would have to add it, for that I leave you this answer of how to create aliases in a basic template link

    
answered by 05.09.2018 в 23:20
0

I've done it based on the yii2 guide link , I think that the problem is in the place where you keep the image that is not a public directory, try saving it in the @web directory ... I hope your contribution is useful ... Regards ...

    
answered by 15.09.2018 в 15:05