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

  • It should work, or maybe the example is not complete (especially in terms of the view). Why do you need parameters and validation for passport if only human is filled in the view? - kroder
  • yes in general, he doesn’t see models i.imgur.com/GEQ1csK.png - des1roer
  • then in the controller to see what objects are created there - kroder
  • there models are normally displayed - des1roer

1 answer 1

the problem is that you also need to pass parameters in the views / create.php file

 <?= $this->render('_form', [ 'model' => $model, 'model_' => $model_ ]) ?> 
  • I wrote to you in the first comment that the example is most likely not complete in terms of the view) there was no second render - kroder
  • @kroder and always, people shorten the code, in the hope that telepathy will still win - Alexey Shimansky