FileValidator rules maxFiles
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
POSTMAN send
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
Uploading more than 1 file add []
Additional info
| Q | A |
|---|---|
| Yii version | 2.0.48.1 |
| PHP version | 7.4 |
| Operating system | windows Openserver 5.4.3 |
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.
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.
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.
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
If you use the UploadedFile::getInstanceByName method, then it will not see any file at all.
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
My suggestion is to change the \yii\validators\FileValidator::validateAttribute line 208. It's like an option.
@bizley do we have a decided way to proceed with this one?
Here I would change the logic to not assume that if maxFiles = 1 then $attribute must not be an array.
@FlatronBuda, @bizley do you have time to prepare a pull request?
I can take it next week hopefully, if @FlatronBuda has time for this sooner it would be great.