S
S
Sergey2015-03-20 21:09:20
Yii
Sergey, 2015-03-20 21:09:20

How to set up image validation in yii2?

There is a form model:

<?php

namespace app\models;

use Yii;
use yii\base\Model;

class Profileavatarform extends Model {
  public $file;
  

  public function rules() {
    return [
      ['file', 'file'],
    ];
  }
}

How to configure rules() to validate it according to the following criteria:
1. Image
only 2. Square image only
3. Height or width no more than 512px
4. Restriction on upload size, for example, no more than 500 kb
If all parameters cannot be validated, I would be glad validation of at least a few items.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-03-23
@butteff

public function rules() {
  return [
    ['file', 'file', 'extensions' => ['png', 'jpg', 'gif'], 'maxSize' => 1024 * 1024 * 2],
  ];
}

Not all, of course, but most of what I wanted is checked.
These are extensions, the maximum size is 1024x1024 and the weight is 2 mb.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question