I do for example https://stackoverflow.com/questions/28567736/yii2-multiple-forms-in-a-single-action
two models - human and passport
controller
public function actionCreate() { $human = new Human(); $passport = new Passport; if ($human->load(Yii::$app->request->post()) && $passport->load(Yii::$app->request->post()) && Model::validateMultiple([$human, $passport])) { $human->save(false); // skip validation as model is already validated $passport->id = $human->passport_id; // no need for validation rule on user_id as you set it yourself $passport->save(false); return $this->redirect(['view', 'id' => $human->id]); } else { return $this->render('create', [ 'human' => $human, 'passport' => $passport, ]); } /* if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', [ 'model' => $model, ]); }*/ } view
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; use app\modules\proj\models\Passport; /* @var $this yii\web\View */ /* @var $model app\modules\proj\models\Human */ /* @var $form yii\widgets\ActiveForm */ ?> <div class="human-form"> <?php $form = ActiveForm::begin([ 'id' => $human->isNewRecord ? 'human-form-create' : 'human-form-update', ]); ?> <?= $form->field($human, 'name')->textarea(['rows' => 6]) ?> <?= $form->field($human, 'passport_id')->textInput() ?> <div class="form-group"> <?= Html::submitButton($human->isNewRecord ? 'Create' : 'Update', ['class' => $human->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?> </div> getting
Call to a member function formName () on a non-object