[solved] Multi Model Form

I’m taking the plunge to Yii2 and it has been a rather difficult one. I have been failing…miserably…even with super simple things like two models in one form.

How do you use multiple models in one form?

I keep getting an error:[size="2"] Call to a member function formName() on null[/size]

[i]UPDATE:

This error was because i wasn’t including the second model in the create.php and passing it to the render _form.php.[/i] i.e.


<?=

	$this->render('_form', [

    	'model' => $model,

    	'FooBarModel' => $FooBarModel

	])

	?>

My controller


public function actionCreate() {

    	$model = new Foo();

    	$FooBarModel = new FooBar();

    	if ($model->load(Yii::$app->request->post()) && $FooBarModel->load(Yii::$app->request->post()) && Model::validateMultiple([$model, $FooBarModel])) {

        	$model->save(false); // skip validation as model is already validated

        	$FooBarModel->event_id = $model->id;

        	$FooBarModel->test_id = $FooBarModel->name;

   		$FooBarModel->save();

        	return $this->redirect(['view', 'id' => $model->id]);

    	} else {

        	return $this->render('create', [

                    	'model' => $model,

                    	'FooBarModel' => $FooBarModel,

        	]);

    	}

	}

My Form




<?php $form = ActiveForm::begin(); ?>


 <?= $form->field($model, 'name')->textInput(['maxlength' => 155]) ?>

 <?= $form->field($model, 'location_id')->dropDownList(ArrayHelper::map(Locations::find()->all(), 'id', 'name')); ?>


<?= $form->field($foobar, 'name')->dropDownList(ArrayHelper::map(Test::find()->all(), 'id', 'name')); ?>


<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>


<?php ActiveForm::end(); ?>

[i]UPDATE:

You will also need to include the base model for validation of multiple models in your controller.


use yii\base\Model; 

The code above does seem to be working. I updated the things that needed to be changes so other people can benefit from my mistakes!

[/i]

I still haven’t figured this out. I’ve tried even just putting two forms on the same page and still get the same error.

is your form in create.php or you include _form.php?

thanks you indirectly helped me figure this out. I was being an idiot and wasn’t passing the second model to the _form.php render in create.php.

1 Like

That was my plan;) I’ve made this mistake many times.

Thanks for sharing and the follow up posting the solution. It will be useful to a lot of people.

hello,

I have followed your code in my application(model TblDataStaff has_many TblDataHobby),

in my application is not work, it cant save my input. how it should be?

thank you.

here’s controller




...

use yii\base\Model;

use app\models\TblDatastaff;

use app\models\TblDataHobby;

...

public function actionCreate()

    {

        $model = new TblDataStaff();

        $model_hob = new TblDataHobby();


        if ($model->load(Yii::$app->request->post()) && $model_hob->load(Yii::$app->request->post()) && Model::validateMultiple([$model, $model_hob])) {

          $model->save(false); // skip validation as model is already validated

          $model_hob->id_staff = $model->id;

          $model_hob->save();

          $model_hob->is_delete = $model->is_delete;

            return $this->redirect(['view', 'id' => $model->id]);

        } else {

            return $this->render('create', [

                'model' => $model,

                'model_hob' => $model_hob,

            ]);

        }

    }



here’s create




<?php


use yii\helpers\Html;

use yii\base\Model;

....


<div class="tbl-data-staff-create">


    <h1><?= Html::encode($this->title) ?></h1>


    <?= $this->render('_form', [

        //'model' => $model,

        'model' => $model,

        'model_hob' => $model_hob,

    ]) ?>


</div>



here’s _form




<?php


use yii\helpers\Html;

use yii\widgets\ActiveForm;

use yii\base\Model;


/* @var $this yii\web\View */

/* @var $model app\models\TblDatastaff */

/* @var $form yii\widgets\ActiveForm */

?>


<div class="tbl-data-staff-form">


    <?php $form = ActiveForm::begin(); ?>


    <?= $form->field($model, 'nip')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>


     <?= $form->field($model_hob, 'name_hobby')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model_hob, 'desc')->textInput(['maxlength' => true]) ?>




    <div class="form-group">

      <?= Html::submitButton($model->isNewRecord ? Yii::t('app','Create') : Yii::t('app','Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    </div>


    <?php ActiveForm::end(); ?>


</div>