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;
}
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']])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With