V
V
Victor Umansky2017-06-20 15:56:34
Yii
Victor Umansky, 2017-06-20 15:56:34

Modal registration not working?

Hello comrades.
When in the modal window in the form I enter the mail and password (then when the mail is entered, the data already appears in the tables) and I click to register, it says to me that the soap is already taken by the
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->session->setFlash('success', 'Письмо с активацией отправлено на емайл <strong>' . Html::encode($user->email) . '</strong> (проверьте папку спам).');
                    } else {
                        Yii::$app->session->setFlash('error', 'Ошибка. Письмо не отправлено.');
                        Yii::error('Ошибка отправки письма.');
                    }
                }
            } else {
                Yii::$app->session->setFlash('error', 'Возникла ошибка при регистрации.');
                Yii::error('Ошибка при регистрации');
                Yii::$app->response->format = Response::FORMAT_JSON;
                return ActiveForm::validate($model);
//                Yii::$app->session->setFlash('error', 'Возникла ошибка при регистрации.');
//                Yii::error('Ошибка при регистрации');
//                return $this->refresh();
            }
        }

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

model
<?php

namespace app\models;

use Yii;
use yii\base\Model;

class Signup extends Model
{
    public $name;
    public $surname;
    public $email;
    public $password;
    public $status;

    /**
     * @return array
     */
    public function rules()
    {
        return [
            [['email', 'password'], 'filter', 'filter' => 'trim'],
            [['email', 'password'], 'required'],
            ['password', 'string', 'min' => 6, 'max' => 255],
            ['email', 'email'],
            ['email', 'unique', 'targetClass' => 'app\models\User', 'message' => 'Эта почта уже занята'],
            ['status', 'default', 'value' => User::STATUS_ACTIVE, 'on' => 'default'],
            ['status', 'in', 'range' => [User::STATUS_NOT_ACTIVE, User::STATUS_ACTIVE]],
            ['status', 'default', 'value' => User::STATUS_NOT_ACTIVE, 'on' => 'emailActivation'],
        ];
    }

    /**
     * @return array
     */
    public function attributeLabels()
    {
        return [
            'email' => 'Почта',
            'password' => 'Пароль',
        ];
    }

    /**
     * Регистрация юзера
     *
     * @return User|null
     */
    public function signUp()
    {
        $user = new User();
        $user->name = $this->name;
        $user->surname = $this->surname;
        $user->role = User::ROLE_USER;
        $user->email = $this->email;
        $user->status = $this->status;
        $user->setPassword($this->password);
        $user->generateAuthKey();
        if ($this->scenario == 'emailActivation')
            $user->generateSecretKey();
        return $user->save() ? $user : null;
    }

    /**
     * Отправка активационного письма
     *
     * @param $user
     * @return bool
     */
    public function sendActivationEmail($user)
    {
        return Yii::$app->mailer->compose('activationEmail', ['user' => $user])
            ->setFrom([Yii::$app->params['supportEmail'] => 'Привет'])
            ->setTo($this->email)
            ->setSubject('Активация аккаунта')
            ->send();
    }

}

view
<?php

use yii\widgets\ActiveForm;
use yii\bootstrap\Html;

/**
 * @var $this yii\web\View
 * @var $form yii\widgets\ActiveForm
 */

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

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

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

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

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Timofeev, 2017-06-20
@webinar

It works for you return $this->renderAjaxSo look in the controller why it comes to it. And I understand you ajax is waiting for errors and not renderAjax

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question