Sourabh Shah
Sourabh Shah

Reputation: 172

Yii2: How to stay or redirect to different page depending on which form is submitted

Both models have the same base model found by $id but I want to redirect to view page if model $lead_status is submitted or to index page if model $lead_transfer is submitted.

$lead_transfer = $this->findModel($id);
$lead_status = $this->findModel($id);
if ($lead_transfer->load(Yii::$app->request->post())&&$lead_transfer->save())
{
    return $this->redirect('index');

}
if ($lead_status->load(Yii::$app->request->post())&&$lead_status->save())
{
    $lead_status->save();
    return $this->refresh();
} 
return $this->render('view', [
    'model' => $this->findModel($id),
    'form_model' =>$form_model,
    'lead_transfer'=>$lead_transfer,
    'lead_status'=>$lead_status,
]);

Upvotes: 0

Views: 326

Answers (1)

Azraar Azward
Azraar Azward

Reputation: 1624

You can keep a hidden field in the form to see which form is submit and redirect accordingly

If (\Yii::$app->request->isPost) {
   switch (\Yii::$app->request->post('hidden')) {
      case 'form_1':
      return $this->redirect(['user/index']);
      case 'form_2':
      return $this->redirect(['user/view']);

   }
}

Upvotes: 2

Related Questions