yii2 - Yii 2 FileValidator -


i'm new yii 2 , reading documentation , experimenting. using activeform , trying file upload, keep getting error "please upload file" though appears file has been uploaded when step through code.

model code

public function upload() {     if ($this->validate())     {         $destination = yii::getalias('@app/uploads');          $this->brochure->saveas($destination . '/' . $this->product_id . '.' . $this->brochure->extension);         return true;     }     else     {         return false;     } } 

controller code

public function actionupdate($id) {     $model = $this->findmodel($id);      if (yii::$app->request->ispost)     {         $model->load(yii::$app->request->post());         $upload = uploadedfile::getinstance($model, 'brochure');          if ($upload !== null)         {             $model->brochure = $upload;             $result = $model->upload();         }          // if model saved, update product_to_language db table language checkboxes         if ($model->save())         {             // first delete old values in product_to_language db table             $cmd = yii::$app->db->createcommand()                 ->delete('product_to_language', "product_id = $model->product_id")             ;             $result = $cmd->execute();              // next update product_to_language db table             $form = yii::$app->request->post('product');              if (!empty($languages = $form['languages']))             {                 // create array of values batch insert                 foreach ($languages $language_id)                 {                     $insert_array[] = [$model->product_id, $language_id];                 }                  $cmd = yii::$app->db->createcommand()                     ->batchinsert('product_to_language',                         ['product_id', 'language_id'],                         $insert_array                     )                 ;                 $result = $cmd->execute();             }              return $this->redirect(['view', 'id' => $model->product_id]);         }     }      return $this->render('update', [         'model' => $model,     ]); } 

_form code

<?php use app\models\product; use app\models\brand; use app\models\language; use app\models\color; use app\models\gender; use yii\helpers\html; use yii\widgets\activeform;  /* @var $this yii\web\view */ /* @var $model app\models\product */ /* @var $form yii\widgets\activeform */ ?>   <div class="admin-product-form">  <?php $form = activeform::begin(     [         'enableajaxvalidation' => false,         'options' => ['enctype' => 'multipart/form-data'],     ] ); ?>  <?php     $errors = $form->errorsummary($model);     echo $errors; ?>  <?= $form->field($model, 'product_name')->textinput() ?>  <?= $form->field($model, 'brand_id')->label('brand')->dropdownlist(brand::getoptions(), ['prompt' => 'select brand']) ?>  <?= $form->field($model, 'price')->textinput() ?>  <?php $model->isnewrecord ? $model->color_id = 1 : $model->color_id = $model->color_id ; ?> <?= $form->field($model, 'color_id')->dropdownlist(color::getoptions()) ?>  <?= $form->field($model, 'gender_id')->radiolist(gender::getoptions(), ['prompt' => 'select gender']) ?>  <?= $form->field($model, 'languages')->checkboxlist(language::getoptions()) ?>  <?= $form->field($model, 'description')->textarea(['rows' => 6, 'class' => 'tinymce', 'maxlength' => true]) ?>  <?= $form->field($model, 'brochure')->fileinput() ?>  <div class="form-group">     <?= html::submitbutton($model->isnewrecord ? 'create' : 'update', ['class' => $model->isnewrecord ? 'btn btn-success' : 'btn btn-primary']) ?> </div>  <?php activeform::end(); ?>  </div> 

link screen capture https://www.dropbox.com/s/zfa0rj8b4pmqyss/yii%20form%20screen%20capture.png?dl=0

your model should have right rules brouchure this:

public function rules() {     return [         [             'brouchure',             'file',             'extensions' => ['png', 'jpg', 'jpeg', 'svg'],             'maxsize' => 1024 * 1024         ]         ...     ]; } 

note: if null attribute value after load should check rules. attribute should safe load.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -