[[+content_image]]
S
S
Sergey Beloventsev2016-04-26 08:06:28
Yii
Sergey Beloventsev, 2016-04-26 08:06:28

Why doesn't validation pass?

Organize the upload of pictures here is the model

class Image extends \yii\db\ActiveRecord
    {
        public $files;
         public function rules()
        {
            return [
                [[ 'id_actoutrs', 'id_category', 'id_pages', 'id_serial', 'id_user', 'for_home'], 'integer'],
                [['files'], 'file', 'extensions' => 'png, jpg'],
                [['title_alt', 'path', 'name'], 'string', 'max' => 255],
            ];
        }
</php>
 

вот представление

   <code lang="php">
 <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
        <?= $form->field($model, 'title_alt')->textInput(['maxlength' => true]) ?>
        <?= $form->field($model, 'files[]')->fileInput(['multiple' => true]) ?>
        <?= $form->field($model, 'for_home')->radioList([
            '0' => Yii::t('app','NO'),
            '1' => Yii::t('app','YES')
        ]); ?>
</code>

вот контролер

   <code lang="php">
 public function actionCreate()
        {
            $model = new Image();
            $class=Yii::$app->request->get("class");
        $feilds =Yii::$app->request->get("feilds");
            $value=Yii::$app->request->get("value");
            if ($model->load(Yii::$app->request->post())) {
                $model->files = UploadedFile::getInstances($model, 'files');
                foreach ($model->files as $file) {
                    $files_to = TransliteratorHelper::process($file->name, '', 'en');
                    $years=date('Y');
                    $mounts=date('m');
                    $path =0;
                    switch ($class) {
                        case 'category':
                            $path = 'category';
                            break;
                    }
                    if (file_exists(Yii::getAlias('@frontend/web/').$path.'/'.$years.'/'.$mounts.'/')) {
                    } else {
                        mkdir(Yii::getAlias('@frontend/web/').$path.'/'.$years.'/'.$mounts.'/', 0775, true);
                    }
                    $file->saveAs(Yii::getAlias('@frontend/web/').$path.'/'.$years.'/'.$mounts.'/'.$files_to);
                    $model->path=$path.'/'.$years.'/'.$mounts.'/';
                    $model->name = $files_to;
                    $model->save();
                } 
</code>


дебаг показывает такую ошибку 20:26:09.879 info yii\db\ActiveRecord::insert Model not inserted due to validation error.

C:\OpenServer\domains\film.lc\backend\controllers\ImageController.php (98)

  <code lang="php">
  print_r( $model->getErrors()).
</code>

выдает такую ошибку Array ( [files] => Array ( [0] => Загрузите файл. ) ) 1
подскажите в чем проблема

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2016-04-26
@webinar

Well, it says: "Upload the file." Your files field should be a file, and judging by the form and name= 'files[]' - you get an array. An array is not equal to a file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question