Yii Validator version 2.6.0 was released.
This release adds a new File validator, extends Image validator input support, and includes translation,
documentation, and internal code quality improvements.
File validator ¶
The new File rule validates that a value is a file. It supports:
- string file paths;
SplFileInfoinstances;- PSR-7
UploadedFileInterfaceinstances.
It can also check allowed extensions, MIME types, and file size.
use Yiisoft\Validator\Rule\File;
use Yiisoft\Validator\Validator;
$result = (new Validator())->validate(
['avatar' => $request->getUploadedFiles()['avatar'] ?? null],
[
'avatar' => new File(
extensions: ['jpg', 'png', 'webp'],
mimeTypes: ['image/jpeg', 'image/png', 'image/webp'],
maxSize: 2_000_000,
),
],
);
For multiple files, use it together with Each:
use Yiisoft\Validator\Rule\Each;
use Yiisoft\Validator\Rule\File;
$rules = [
'attachments' => new Each(
new File(
extensions: ['pdf', 'txt'],
maxSize: 5_000_000,
),
),
];
Optional upload fields can use skipOnEmpty:
use Yiisoft\Validator\Rule\File;
$rule = new File(skipOnEmpty: true);
SplFileInfo support in Image validator ¶
Image validator now accepts SplFileInfo values directly. This is useful when your application already works with
filesystem objects instead of plain paths.
use SplFileInfo;
use Yiisoft\Validator\Rule\Image\Image;
use Yiisoft\Validator\Validator;
$result = (new Validator())->validate(
new SplFileInfo(__DIR__ . '/avatar.jpg'),
new Image(
minWidth: 128,
minHeight: 128,
maxWidth: 2048,
maxHeight: 2048,
),
);
The Image validator was also fixed to handle unreadable streams correctly.
Other changes ¶
- Updated Polish translations.
- Explicitly imported classes, functions, and constants in use sections.
- Fixed translations, documentation grammar, incorrect imports, and a broken contributing guide link.