M
M
Mikha Pankratov2015-03-12 15:06:14
JavaScript
Mikha Pankratov, 2015-03-12 15:06:14

Yii2 - Problem with registration?

Hello everyone, I have a problem with registration. I immediately check the validation (it seems to work normally)

I send it with Ajax

var singup = (function () { 
    var name = $("#name").val();
    var lastName = $("#last-name").val();
    var psw = $("#pwd").val();
    var email = $("#mail").val();
    if(validName(name) && validLastName(lastName) && validEmail(email) && validPsw(psw)){
        $.ajax({
            type: 'POST',
            url: "site/singup",
            data: {"name": name, "lastname": lastName, "psw":psw, 'login':email},
            dataType: "json",
            success: function(data){
                if (data.success == true){
                    window.location.href = data.redirect_uri;
                } else $(".r-f-pp-l .p-att").html(data.msg).addClass('error-msg');
            },
            error: function(e) {
                alert('Error');
//                alert(e);
            }
          });
    }
});


It comes to the controller and here it’s not clear that it adds something, then it throws an error%)
public function actionSingup()
    {
         if (Yii::$app->request->isAjax) {
            $name = Yii::$app->request->post('name');
            $lastname = Yii::$app->request->post('lastname');
            $psw = Yii::$app->request->post('password');
            $email = Yii::$app->request->post('login');
            
            if(User::find()->where(array('email'=>Yii::$app->request->post('login')))->asArray()->one()){
                return json_encode(array("success" => false, "msg"=>"Такой емайл уже загерестрирован"));
            }  else {
                $user = new User();
                $user->username = $name;
                $user->lastname = $lastname;
                $user->email = $email;
                $user->setPassword($psw);
                $user->status = User::STATUS_ACTIVE;
                $user->generateAuthKey();
                $user->generateEmailConfirmToken();
                if ($user->save()) {
                    Yii::$app->mailer->compose('confirmEmail', ['user' => $user])
                    ->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])
                    ->setTo($this->email)
                    ->setSubject('Email confirmation for ' . Yii::$app->name)
                    ->send();   
                    return json_encode(array("success" => true, "redirect_uri" => '/'));    
                }else{
                    return json_encode(array("success" => false, "msg" => 'Исправьте ошибки.'));                    
                }
            }
         }
    }

Tell me where is the mistake

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Semenko, 2015-03-12
@abler98

Why is asArray() here?

User::find()->where(array('email'=>Yii::$app->request->post('login')))->asArray()->one()

M
Mikha Pankratov, 2015-03-12
@frmax

Well, yes) you can just an object

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question