title: Uploading image and Validation in Yii2
body: <p>I'm using Yii2 to create a simple uploading image and validate images extension. I've read in Yii2 docs and I don't know <code>$model-&gt;validate()</code> always return <code>false</code>. Any idea for me?</p>
  
  <p>In my Model file, I set rules for <code>imageFile</code>:</p>
  
  <pre><code>public function rules()
      {
          return [
              [['name', 'price', 'description', 'image_path'], 'required'],
              [['price'], 'integer'],
              [['description'], 'string'],
              [['name', 'image_path'], 'string', 'max' =&gt; 255],
              [['imageFile'], 'image', 'skipOnEmpty' =&gt; false, 'extensions' =&gt; 'png, jpg, jpeg'],
          ];
      }
  </code></pre>
  
  <p>And in my action Index of Controller:</p>
  
  <pre><code>if ($model-&gt;load(Yii::$app-&gt;request-&gt;post())) {
      $model-&gt;imageFile = UploadedFile::getInstance($model, 'imageFile');
      if ($model-&gt;validate() &amp;&amp; $model-&gt;save()) {
          $model-&gt;imageFile-&gt;saveAs('uploads/' . Yii::$app-&gt;params['bonuslyRewards'] . '/' . $model-&gt;imageFile-&gt;baseName . '.' . $model-&gt;imageFile-&gt;extension);
          $model-&gt;image_path = 'uploads/' . Yii::$app-&gt;params['bonuslyRewards'] . '/' . $model-&gt;imageFile-&gt;baseName . '.' . $model-&gt;imageFile-&gt;extension;
          return $this-&gt;redirect(['view', 'id' =&gt; $model-&gt;id]);
              }
      } else {
        return $this-&gt;render('create', [
              'model' =&gt; $model,
          ]);
      }
  </code></pre>