yii-dream-team / yii2-upload-behavior

Yii2 file/image upload behavior for ActiveRecord
http://yiidreamteam.com/yii2/upload-behavior
MIT License
77 stars 45 forks source link

File Instances are not recognized when using array of file_input. #13

Open phdcoder opened 8 years ago

phdcoder commented 8 years ago

I have a page with several models, arranged as array, and they have a file_input control. But the attribute never gets the UploadedFile object because of differences in the name of the files in the $_FILES array. I got around this problem by using a third check in BeforeValidade event. Maybe you guys want to use this solution in yii2-upload-behavior.

    if (empty($this->file)) {
        $this->file = UploadedFile::getInstanceByName($this->attribute);
        if (empty($this->file))
        {
            $id=$this->owner->id;
            $att=$this->attribute;
            $name= $this->owner->formName()."[$id][$att]";
            $this->file = UploadedFile::getInstanceByName($name); 
        }

   }

    if ($this->file instanceof UploadedFile) {
        $this->owner->{$this->attribute} = $this->file;
    }
}