yii2-upload-behavior icon indicating copy to clipboard operation
yii2-upload-behavior copied to clipboard

How to use with separate FormModel?

Open Matvik opened this issue 4 years ago • 4 comments

I don`t clearly understand, what we shold do, if there are separate FormModel, with the help of which the main ActiveRecord model is manipulated. Can i see some example? Thanks

Matvik avatar Jul 14 '20 17:07 Matvik

Any news? This extention is very helpfull(

Matvik avatar Sep 09 '20 14:09 Matvik

I dont clearly understand, what we shold do, if there are separate FormModel, with the help of which the main ActiveRecord model is manipulated. Can i see some example? Thanks My example

namespace common\modules\catalog\models;

use yii\base\Model; use yii\web\UploadedFile;

class PhotosForm extends Model { /** * @var UploadedFile[] */ public $files;

public function rules(): array
{
    return [
        [['files'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, jpeg, JPG, JPEG', 'maxFiles' => 10],

    ];
}

public function beforeValidate(): bool
{
    if (parent::beforeValidate()) {
        $this->files = UploadedFile::getInstances($this, 'files');
        return true;
    }
    return false;
}

public function upload($product_id)
{
    if ($this->validate()) {
        foreach ($this->files as $file) {
            $modelPic = new Pic(['file'=>$file]);
            $modelPic->product_id = $product_id;
            try {
                $modelPic->save();
            } catch (\DomainException $e) {
                \Yii::$app->errorHandler->logException($e);
                \Yii::$app->session->setFlash('error', $e->getMessage());
            }
        }
        return true;
    } else {
        return false;
    }
}

}`

yamalweb avatar Sep 15 '20 05:09 yamalweb

I dont clearly understand, what we shold do, if there are separate FormModel, with the help of which the main ActiveRecord model is manipulated. Can i see some example? Thanks My example

namespace common\modules\catalog\models;

use yii\base\Model; use yii\web\UploadedFile;

class PhotosForm extends Model { /**

  • @var UploadedFile[] */ public $files;
public function rules(): array
{
    return [
        [['files'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, jpeg, JPG, JPEG', 'maxFiles' => 10],

    ];
}

public function beforeValidate(): bool
{
    if (parent::beforeValidate()) {
        $this->files = UploadedFile::getInstances($this, 'files');
        return true;
    }
    return false;
}

public function upload($product_id)
{
    if ($this->validate()) {
        foreach ($this->files as $file) {
            $modelPic = new Pic(['file'=>$file]);
            $modelPic->product_id = $product_id;
            try {
                $modelPic->save();
            } catch (\DomainException $e) {
                \Yii::$app->errorHandler->logException($e);
                \Yii::$app->session->setFlash('error', $e->getMessage());
            }
        }
        return true;
    } else {
        return false;
    }
}

}`

And using this behavior?

Matvik avatar Jan 21 '21 18:01 Matvik

Figured this out. Just attach behavior to the ActiveRecord model ("User", for example), then in UserForm model BEFORE saving User model with new data perform this: $this->user->image = UploadedFile::getInstance($this, 'image'); Then save User as usual.

Matvik avatar Jan 22 '21 17:01 Matvik