简体   繁体   中英

Yii2 user registration

I am using yii2 default login that successfully logs in if i input 'admin' for username and password. 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. 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
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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