yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

FileValidator rules maxFiles

Open FlatronBuda opened this issue 2 years ago • 8 comments

What steps will reproduce the problem?

I am using the multiple file upload feature. $this->files = UploadedFile::getInstancesByName('f[files]'); It is assumed that the rules may indicate that the maximum number of files is 1 or more files. public function rules() { return [ [['files'], 'file', 'skipOnEmpty' => true, 'checkExtensionByMimeType' => true, 'maxSize' => 1024 * 1024 * 7, 'maxFiles' => $this->maxFiles //Dynamic number files ], ]; }

What is the expected result?

Upload and model save is success.

What do you get instead?

If I specify in the rules to download only 1 file and at the same time I attach the file. When validating, I get an error as if there is no file. Although in xdebug it sees in the $_FILES variable.

Xdebug show $_FILES image

POSTMAN send image

If you specify more than 1, the validator works fine. Similar question

Solution 1

Change core code in File \yii\validators\FileValidator::validateAttribute if ($this->maxFiles != 1 || $this->minFiles > 1) { to if (!$model->$attribute instanceof UploadedFile && ($this->maxFiles >= 1 || $this->minFiles > 1)) {

Solution 2

Change my code FileForm model Look count maxFiles. Depending on the number of usage files, the corresponding function public function load($data, $formName = null) { if($this->maxFiles==1){ $this->files = UploadedFile::getInstanceByName('f[files]'); }else{ $this->files = UploadedFile::getInstancesByName('f[files]'); } if (parent::load($data, $formName)) { return true; } return false; } Modify Handler my file $filesListCheck = []; if($this->files instanceof UploadedFile){ $filesListCheck[]=$this->files; }else{ $filesListCheck=$this->files; }

With 2 solutions, you have to specify in postman. When uploading 1 file image Uploading more than 1 file add [] image

Additional info

Q A
Yii version 2.0.48.1
PHP version 7.4
Operating system windows Openserver 5.4.3

FlatronBuda avatar Jun 03 '23 05:06 FlatronBuda

Definitely if you want to have more files you need to set the property holding them as an array. I would like to see your code for handling the file upload and validation please.

bizley avatar Jun 03 '23 09:06 bizley

Definitely if you want to have more files you need to set the property holding them as an array. I would like to see your code for handling the file upload and validation please.

code.txt

FlatronBuda avatar Jun 09 '23 15:06 FlatronBuda

So if I understand correctly, you want to change FileValidator to not throw errors when you pass array of files but with maxFiles = 1 and array containing only 1 file? This sounds reasonable but I would change not the line you mentioned but rather line 273.

bizley avatar Jun 09 '23 16:06 bizley

So if I understand correctly, you want to change FileValidator to not throw errors when you pass array of files but with maxFiles = 1 and array containing only 1 file? This sounds reasonable but I would change not the line you mentioned but rather line 273.

Yes, I want multiple file uploads. With a maximum of 1 file. Use the UploadedFile::getInstancesByName method. The problem is that the validator is configured with more than 1 file \yii\validators\FileValidator::validateAttribute line 208

image

If you use the UploadedFile::getInstanceByName method, then it will not see any file at all. image

In some places I need to attach only 1 file, and somewhere a maximum of 7 files, for example. Using 1 model (FilesDemoForm) with maxFiles=1 in the rules

image My suggestion is to change the \yii\validators\FileValidator::validateAttribute line 208. It's like an option.

FlatronBuda avatar Jun 09 '23 17:06 FlatronBuda

@bizley do we have a decided way to proceed with this one?

samdark avatar May 16 '24 10:05 samdark

Here I would change the logic to not assume that if maxFiles = 1 then $attribute must not be an array.

bizley avatar May 16 '24 10:05 bizley

@FlatronBuda, @bizley do you have time to prepare a pull request?

samdark avatar May 16 '24 11:05 samdark

I can take it next week hopefully, if @FlatronBuda has time for this sooner it would be great.

bizley avatar May 16 '24 14:05 bizley