Different form for create and update in the same model

Hello,

I’ve modified the form of a Model to act as a batch form (creates n models based on what the user needs), to achieve this I modified the actionCreate, so far so good.

But when the user later has to modify one of those models I want to show a form that is limited to just some of the elements of the form, and the rest of values are not displayed and remain unmodified in the update. And as you know, by default when clicking the update button the same form for creating is shown.

How can I achieve that? Do I need to create a separate form2 and redirect to it at the moment of clicking "update", if so: how?

Hope the questions is clear enough.

Nevermind, It is as simple as:

  1. create another form with only the fields you need to modify.

  2. in the views folder of the model, open the update.php file.

  3. in the part where you call the form to render, change it to the alternate form for update that you made in step 1 (eg: form2.php)




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

        'model' => $model,

    ]) ?>



Hope it will be usefull to someone.

Individual form fields can be displayed conditionally based on yii\db\ActiveRecord::$isNewRecord or yii\base\Model::isAttributeSafe().