Answer the question
In order to leave comments, you need to log in
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);
}
});
}
});
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" => 'Исправьте ошибки.'));
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Why is asArray() here?
User::find()->where(array('email'=>Yii::$app->request->post('login')))->asArray()->one()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question