There is an Item controller, an actionCreate and a handler in it.

public function actionCreate() { $item = new Item(); $file = new File(); $file->trigger(File::FILE_UPLOADS); if ( Yii::$app->request->isAjax && $item->load( Yii::$app->request->post() ) ) { $item->attributes = Yii::$app->request->post('Item'); $errors = ActiveForm::validate( $item ); } if( $errors ) { Yii::$app->response->format = Response::FORMAT_JSON; return $errors; } } 

And the File model

 class File extends \yii\db\ActiveRecord { const FILE_UPLOADS = 'file-uploads'; public function init() { $this->on(self::FILE_UPLOADS, [$this, 'uploads']); } public function uploads( $event ) { $file = new File(); if ( Yii::$app->request->isAjax && $file->load( Yii::$app->request->post() ) ) { $file->image = UploadedFile::getInstance($file, "image"); } $errors = ActiveForm::validate( $file ); return $errors; } } 

The file is submitted for validation, everything seems to be ok, but I don’t understand how I can return the result back to the Item controller, i.e. is this part?

 $errors = ActiveForm::validate( $file ); 

Ideally, I want to completely separate Item and File, but for now I don’t understand how, please tell me.

    1 answer 1

    Perhaps your problem should be solved like this:

     сlass ItemController extends yii\web\Controller { public function actionCreate() { $item = new Item(); $file = new File(); $file->trigger(File::FILE_UPLOADS); Yii::$app->getResponse()->format = Response::FORMAT_JSON; if (Yii::$app->getRequest()->isAjax()) { return false; } if ($item->load(Yii::$app->getRequest()->post()) && $file->load(Yii::$app->getRequest()->post())) { if (!$item->validate()) { return \yii\helpers\Html::errorSummary($item); } if (!$file->validate()) { return \yii\helpers\Html::errorSummary($file); } //Ваш код... } } } 

    That is, to validate the model. Also why do you inherit the File model from \ yii \ db \ ActiveRecord, and not from yii \ base \ Model?