pgaultier / sweelix-yii2-plupload

Sweelix Yii2 plupload extension - easy asynchronous file uploads
Other
17 stars 9 forks source link

Validation #7

Closed Kison closed 7 years ago

Kison commented 8 years ago

Does anybody know how to validate files with this extension?

MaximusBaton commented 7 years ago

You need to make a custom validator, since ->tempName is referring to another location

Example

<?php
namespace app\components\validators;

use Yii;
use yii\validators\FileValidator as BaseValidator;

class PluploadFileValidator extends BaseValidator
{

    /**
     * Checks if given uploaded file have correct type (extension) according current validator settings.
     * @param UploadedFile $file
     * @return bool
     */
    protected function validateExtension($file)
    {
        $oldTempName = $file->tempName;

        $file->tempName = Yii::getAlias( \sweelix\yii2\plupload\components\UploadedFile::$targetPath ) . DIRECTORY_SEPARATOR . $file->tempName;

        $result = parent::validateExtension($file);

        $file->tempName = $oldTempName;
        return $result;
    }
}

In a model

    public function rules()
    {
        return array_merge(parent::rules(), [
          ...
          ['uploadedFile', 'app\components\validators\PluploadFileValidator', 'extensions' => ['jpg', 'png', 'jpe', 'jpeg'], 'maxFiles' => 1, 'maxSize' => 1024],
          ...
        ]);
    }
Kison commented 7 years ago

Thanks )