简体   繁体   English

Yii2用户注册

[英]Yii2 user registration

I am using yii2 default login that successfully logs in if i input 'admin' for username and password. 我正在使用yii2默认登录名,如果我输入“ admin”作为用户名和密码,则该登录名将成功登录。 The problem is with new user registration. 问题在于新用户注册。 I have this controller action to register the username and password. 我有此控制器操作来注册用户名和密码。

public function actionSignup()
    {
        $model = new User();


    if(isset($_POST['User']))
    {
        $model->attributes=$_POST['User'];

        $model->setAttributes(array(
            'datereg' => time(), 
            'lastlogin' => time()   
        ));

        if($model->save())
        {

            $login=new LoginForm;
            $login->username = $_POST['User']['username'];
            $login->password = $_POST['User']['password'];
            if($login->validate() && $login->login())
                $this->redirect('/telephone/add');
        }

    }
    else
 {
        return $this->render('register');
 }
    }

the login action is: 登录操作为:

 public function actionLogin()
    {
        if (!\Yii::$app->user->isGuest) {
            return $this->redirect(Yii::$app->request->baseUrl.'/telephone/add');   
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->redirect(Yii::$app->request->baseUrl.'/telephone/add');   
        }
        return $this->render('login', [
            'model' => $model,
        ]);
    }

user and loginform are the default models. userloginform是默认模型。 The registration form is like: 注册表如下:

<div class="container">
<div class="row">
<h2> New User Signup</h2><br>
    <form class="formclass" method="POST" action="<?php echo Yii::$app->request->baseUrl;?>/telephone/signup/" role="form"  id="register-form" novalidate="novalidate">
                      <label> UserName:</label>     <input type="name" name="username" id="username" placeholder="UserName" required><br><br>
                      <label>    Password: </label>       <input type="password" name="password" id="password" placeholder="Password" required><br><br>
           <button type="submit" class="btn btn-default">Sign Up</button><BR><BR>
                            </form>

</div>
</div>

But the username and password are not saving in database and there are no errors. 但是用户名和密码未保存在数据库中,并且没有错误。 Also it is not redirecting after successful registration.When i clik sign up button, the page is like reloading. 成功注册后也不会重定向。当我点击“注册”按钮时,页面就像重新加载一样。

Could be some validation rules don't match 可能是某些验证规则不匹配

Try perform a proper validation before save 保存前尝试执行适当的验证

if(isset($_POST['User']))
{
    $model->attributes=$_POST['User'];

    // validate user input and redirect to the previous page if valid
    $model->setAttributes(array(
        'datereg' => time(), //additional data you want to insert
        'lastlogin' => time() //additional

    ));
    if ($model->validate()) {
         if($model->save()) {

         ......your code 

         }
    } else {
        // validation failed: $errors is an array containing error messages
        $errors = $model->errors;
        var_dump($error)
    }

Based on the weird fact that $errors seems not work (see this doc for ref http://www.yiiframework.com/doc-2.0/guide-input-validation.html ) then you should remove from the code the else conditio for $errors and you can try 基于$ errors似乎不起作用的怪异事实(请参阅此文档,以获取参考资料http://www.yiiframework.com/doc-2.0/guide-input-validation.html ),然后应从代码中删除else条件$错误,你可以尝试
for debuging only 仅用于调试

$model->save(false)  

if in this case your data are saved in db then you have a validation problen and you must selectively comment or remove the valdation rule for finding the one that don't match 如果在这种情况下,您的数据保存在db中,则您有一个验证问题,您必须有选择地注释或删除验证规则以查找不匹配的规则

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM