File Upload From Controller to ActiveRecord


I’m in a bit of a pickle here and hope get help.

The Senario:

I have a multiple product attributes, each has an image associated to it. In submitted product attributes, I segregate the by models to update, models to delete, models to insert.

As a result, I will need to move file upload to the same Active Record Model as well.

Product attributes model is called "ProdAttributes" <- extends active record.

Product attribute images model is called "Upload" <- extends model.

For product attributes to be inserted I added the following code in "ProdAttributes" to handle uploaded images.

$uploader = new Upload();

$uploader->image = Uploader::getInstance($uploader, 'image');

if($uploader->upload($prod_attribute_id)) {

echo 'Uploaded';die;


In Upload model I have the following code:

public function rules() {

return [['image', 'image', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, jpeg']];


public function upload($prod_attribute_id) {

if ( $this->validate() ) {






The Error:

The validation always fails with message "Please upload a file".

The Investigation:

With that error I investigated the follow:

"$uploader->image" contains the image upload object (Sounds good)

When I "var_dump($this)" of "$this->validate()" defined in Upload model returns the attributes defined in the active record of "ProdAttributes" (Not Good). It expected to return only the "image" attribute defined in Upload model.

Any idea on how to get past this?


if you getting ProdAttributes in your upload method, it is likely you are setting the attributes of your upload model somewhere in your code.

can you paste your code with entire method body as well as the controller action code?

Thanks for your reply.

I found the solution for it.

It was that I customized Yii2 default file uploader, and the validation was failing as it was using the classes for Yii2 default UploadedFile class.

The solution was that I created custom validators to use the customized version of the uploader.