P
P
ptand2018-08-24 11:09:40
Yii
ptand, 2018-08-24 11:09:40

Yii2 how to make a redirect in a modal window on successful form validation?

Hello everyone, help me understand, I can not figure out how to implement the idea correctly. In yii2, the idea is to implement a two-step registration in a modal window. Click "Register", a modal window opens with fields

public function actionSignupStepOne()
    {
        $model = new Signup;
        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
            if ($user = $model->save()) {
                return $this->redirect('site/signup-step-two');
            } else {
                Yii::$app->response->format = Response::FORMAT_JSON;
                return ActiveForm::validate($model);
            }
        }
        return $this->renderAjax('signup-step-one', [
            'model' => $model,
        ]);
    }

, after clicking submit and successful validation, you need the second registration form to open in the same modal window, but it just redirects to the page. Tell me how can I implement this? I tried to catch the event to transfer to the success view with successful validation, and in the view already render if the success is true, but nothing happens. Tried afterValidate but also without much success.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2018-08-24
@ptand

Good afternoon.
This line is a redirect to another page . Return some value instead of a redirect, and in 'success' check what is returned. If the desired value is returned, then show the second form. Or try to return the form in the same way as the first

return $this->renderAjax('signup-step-two', [
'model' => $model,
]);

ps Why exactly like this $user = $model->save()?

A
Arman, 2018-08-24
@Arik

It's probably X-Redirect , yii.js sends the browser to a new URL by default.
You can try

return $this->redirect('site/signup-step-two', 302, false);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question