Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create page with different forms in view(yii2)?

Tags:

php

yii2

For example:

We have table book with different types (Horror, Travel, History).

We need to create a method update for these books, but for each book type we need to show different type of form.

How can we achieve this?

Is the following code correct?

public function actionUpdate($id)
{
    $model = $this->getType($id);

    switch ($model->type){
        case Book::TYPE_HORROR:
            return $this->render('update_horror', [
                'model' => $model
            ]);
            break;
        case Book::TYPE_TRAVEL:
            return $this->render('update_travel', [

            ]);
            break;
        case Book::TYPE_HISTORY:
            return $this->render('update_history', [

            ]);
    }

    throw new NotFoundHttpException;
}
like image 718
Fitstd Avatar asked Nov 27 '25 22:11

Fitstd


1 Answers

You can do it something like this

public function actionUpdate($id)
{
    $model = $this->getType($id);

    return $this->render('update_'. $model->type, ['model' => $model]);

}

where view name have to be "update_" + exactely $model->type result,

eg: horror = update_orror.php

This is for reneder the view, but do not forget to add action method to your form, otherwise it will look for a page that doesn't exist.

$form = ActiveForm::begin(['action' =>['book/update']])
like image 56
Gabriele Carbonai Avatar answered Nov 29 '25 13:11

Gabriele Carbonai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!