php - Yii2: Customized LoginForm is not working -


i writing own login form validate user attempts. here code.

loginform.php:

namespace common\models;  use yii; use yii\base\model; use common\models\user;  class loginform extends model{      public $username;     public $password;     public $rememberme = true;     public $verification;      private $user;      public function rules(){         return [             [['username','password'],'trim'],              [['username','password'],'required','message'=>'nothing input yet'],             ['username','validateusername'],             ['rememberme', 'boolean'],             ];     }      public function validateusername($attribute, $params){         $user = user::find()->where(['username'=>$this->$attribute])->asarray()->one();         if(!$user || md5($this->password)!=$user['password']){                 $this->adderror($attribute,'username/password error');             }else{                 $this->user = $user;             }     }  } 

code index:

<div class="container"> <?=html::beginform('','post',['enctype'=>'multipart/form-data','class'=>'form-signin'])?>          <h2 class="form-signin-heading">please sign in</h2>         <?=html::label('username','username',['class'=>'sr-only'])?>         <?=html::input('text','username',$model->username,['id'=>'username','class'=>'form-control','placeholder'=>'username','autofocus'=>true])?>         <?=html::error($model,'username',['class'=>'error'])?>         <?=html::label('password','password',['class'=>'sr-only'])?>         <?=html::input('password','password',$model->password,['id'=>'inputpassword','class'=>'form-control','placeholder'=>'password'])?>         <?=html::error($model,'password',['class'=>'error'])?>         <div class="checkbox">           <label>             <input type="checkbox" value="remember-me"> remember me           </label>         </div>         <?=html::submitbutton('login',['class'=>'btn btn-lg btn-primary btn-block'])?>  <?=html::endform()?> </div> 

here logincontroller.php

namespace backend\controllers;  use yii\web\controller; use common\models\loginform; use yii;   class logincontroller extends controller{      public $layout = 'login' ;      public function actionindex(){          $model = new loginform();          if($model->load(yii::$app->request->post()) && $model->validate()){             return $this->redirect(['site/index']);         }else{             echo "bad";         }          return $this->render('index',['model'=>$model]);     }  } 

now problem is, no matter input or leave blank, there no respond after submit form. show "bad" output. have checked code , couldn't find out wrong. not error message loginform model. ex: 'nothing input yet'.

ps: found there might way called exits haven't tried yet, wanna fix own code first process next built-in method.

attached result output pic. enter image description here

yii2 has built-in method in user component creating user session called login. can access function yii::$app->user->login($user).

here class method added.

// may doing after validating namespace common\models;  use yii; use yii\base\model; use common\models\user;  class loginform extends model{   public $username;  public $password;  public $rememberme = true;  public $verification;   private $user;   public function rules(){     return [         [['username','password'],'trim'],          [['username','password'],'required','message'=>'nothing input yet'],         ['username','validateusername'],         ['rememberme', 'boolean'],         ];  }   public function validateusername($attribute, $params){     $user = user::find()->where(['username'=>$this->$attribute])->asarray()->one();     if(!$user || md5($this->password)!=$user['password']){             $this->adderror($attribute,'username/password error');         }else{             $this->user = $user;         }  }  }  // adding below method public function login(){  if($this->user && yii::$app->user->login($this->user)) return true;  return false;  } 

and calling new method in controller.

if($model->load(yii::$app->request->post()) && $model->validate() && $model->login()){         return $this->redirect(['site/index']); } 

note don't forget add user class inside user component in config.


Comments