E
E
Eugene2017-06-18 17:46:19
Yii
Eugene, 2017-06-18 17:46:19

What is the best way to make a wizard form in yii2?

I have a form where the order of goods of the wizard form type takes place in several stages ...
1) I can implement it in one view through the tab-pane, but how to validate the forms from the transition of one tab to another ... Or is there another solution altogether ?
2) I was advised to do it like "actionCreate", but after a successful save, redirect to the next step of filling out the questionnaire, but then there will be extra 2-3 tables and models ... as a working example below:

public function actionShipping($id)
    {
        $model = new OrderDeliveryMethod();

        $model->order_id = $id;

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['payment', 'id' => $model->id]);
        } else {
            return $this->render('shipping', [
                'model' => $model,
            ]);
        }
    }

    public function actionPayment($id)
    {
        $model = new OrderPaymentMethod();
        $model->order_id = $id;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['review', 'id' => $model->id]);
        } else {
            return $this->render('payment', [
                'model' => $model,
            ]);
        }
    }

    public function actionReview($id)
    {
        return $this->render('review', [
            'model' => $this->findModel($id),
        ]);
    }

It works, but is it the right way to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-06-19
@e236286

1) I can implement it in one view through the tab-pane, but how to validate the forms from the transition of one tab to another ... Or is there another solution altogether?

Do not reinvent the wheel, there are a bunch of ready-made extensions that allow you to do this, for example , this or this . In general, there are a lot of such extensions, just google for example like this
It all depends on the device of your system. If the system provides that the user can stop filling out the questionnaire, and after a couple of days return to the place where he stopped (and all the data he previously entered will be loaded) - then such an implementation takes place. If such a scheme is not provided, it is better to avoid such an implementation. get a bunch of inconsistent data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question