Yii2 rules not apply to non-db attributes?

I would like to upload several files at once. I have

model:

class KakiKingModel extends ActiveRecord {

    public $uploadedFiles;

    public function rules() {
        return [
            [['uploadedFiles'], 'file', 'extensions' => 'txt', 'checkExtensionByMimeType' => false, 'skipOnEmpty' => true, 'maxFiles' => 2]];
    }
    ...

controller:

use yii\web\UploadedFile;
...
public function actionUpload() {
    $model = new KakiKingModel;
    $t = new KakiKingModel;

    if (Yii::$app->request->isPost) {
        $files = UploadedFile::getInstances($model, 'uploadedFiles');

        $t = [];
        $i = 0;
        foreach ($files as $i => $file) {
            $t[$i] = new KakiKingModel;
            $t[$i]->contentUploadedFile = file($file->tempName);
            $t[$i]->assign(); // assign file content to model attributes
            $i++;
        }

        if (Model::validateMultiple($t)) {
            foreach ($t as $item) {
                $item->save(false);
            }
            return $this->redirect(['index']);
        } else {
            return $this->render('upload', [
                        'model' => $model,
                        't' => $t,
            ]);
        }
    }

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

View:

$form = ActiveForm::begin([
    ....
    'options' => ['enctype' => 'multipart/form-data'],
    ...
        <?= $form->field($model, 'uploadedFiles[]')->fileInput(['multiple' => true]) ?>

and the problem is that it accepts other types of files! Why is this? What am I doing wrong? Thank you UPDATE: I changed my content a bit, so you can better understand why I find it disturbing that it does not work. He should work IMHO. could you help me? Thanks!

+4
source share
2 answers

Yii2 validation rules also apply to model attributes without a database.

I think there are two common reasons for this problem:

1) , enctype :

use yii\widgets\ActiveForm;

...

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>

2) yii\validators\FileValidator (aliased file) yii\web\UploadedFile ( PHP- $_ FILES), , .

\yii\web\UploadedFile:: getInstance():

use yii\web\UploadedFile;

...

$this->file = UploadedFile::getInstance($this, 'file');

\yii\web\UploadedFile:: getInstance():

use yii\web\UploadedFile;

...

$this->files = UploadedFile::getInstances($this, 'files');

$model->save() $model->validate() beforeValidate():

/**
 * @inheritdoc
 */
public function beforeValidate()
{
    $this->files = UploadedFile::getInstances($this, 'files');

    return parent::beforeValidate();
}
+4

:

    namespace app\models;

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

    /**
    * UploadForm is the model behind the upload form.
    */
    class UploadForm extends Model
    {
    /**
     * @var UploadedFile|Null file attribute
     */
   public $uploadedFiles;

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            [['uploadedFiles'], 'file'],
        ];
    }
    }
    ?>

:

<?php
use yii\widgets\ActiveForm;

$form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>

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

<button>Submit</button>

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

Source: https://habr.com/ru/post/1625887/


All Articles