V
V
Victor Umansky2017-06-20 22:03:09
Yii
Victor Umansky, 2017-06-20 22:03:09

The data itself is saved in the form without having time to click on the button?

In the modal window, I fill out the registration form and before I even click on the button, it already flies to save, and when I click the button, it says This mail is already busy, in short, registered ....

How to make registration occur after clicking on the button and after if everything is ok, the form was closed and popped up

Yii::$app->session->setFlash('success', 'Письмо с активацией отправлено на емайл <strong>' . Html::encode($user->email) . '</strong> (проверьте папку спам).');


c1bbda9b41cb43b1a10a4c66df7a0f77.png

form
<?php $form = ActiveForm::begin([
    'id' => 'reg-form',
    'enableAjaxValidation' => true,
]); ?>


<?= $form->field($model, 'name')->textInput() ?>
<?= $form->field($model, 'surname')->textInput() ?>
<?= $form->field($model, 'email')->textInput() ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'password_repeat')->passwordInput() ?>


<?= Html::submitButton('Зарегистрироваться') ?>

<?php ActiveForm::end(); ?>


controller
public function actionSignUp()
    {
        $emailActivation = Yii::$app->params['emailActivation'];
        $model = $emailActivation ? new Signup(['scenario' => 'emailActivation']) : new Signup();

        if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()) && $model->validate()) {

            if ($user = $model->signup()) {
                if ($user->status === User::STATUS_ACTIVE) {
                    if (Yii::$app->getUser()->login($user)) {
                        return $this->goHome();
                    }
                } else {
                    if ($model->sendActivationEmail($user)) {
                        Yii::$app->response->format = Response::FORMAT_JSON;
                        Yii::$app->session->setFlash('success', 'Письмо с активацией отправлено на емайл <strong>' . Html::encode($user->email) . '</strong> (проверьте папку спам).');
                        return ActiveForm::validate($model);
                    } else {
                        Yii::$app->session->setFlash('error', 'Ошибка. Письмо не отправлено.');
                        Yii::error('Ошибка отправки письма.');
                    }
                }
            } else {
                Yii::$app->session->setFlash('error', 'Возникла ошибка при регистрации.');
                Yii::error('Ошибка при регистрации');
                return $this->refresh();
            }
        }

        return $this->renderAjax('sign-up', ['model' => $model]);
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2017-06-21
@slo_nik

Goodnight.
Remove 'enableAjaxValidation' => true
Transfer to model&& $model->validate()

M
Maxim Timofeev, 2017-06-23
@webinar

enableAjaxValidation => true
Validation via ajax, so not surprising, but you can set it up on submit

'enableAjaxValidation' => true,
'validateOnBlur' => false,
'validateOnChange' => false,
'validateOnType' => false,
'validateOnSubmit' => true,

This and other goodies are in the docks: www.yiiframework.com/doc-2.0/yii-widgets-activefor...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question