yii2.0:登录界面提交表格后非正常提示 [ 2.0 版本 ]
用户提交登录界面,提交用户表格后输出结果非model里面的message匹配内容?
LoginController.php代码如下:
<?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{
var_dump(Yii::$app->request->isPost);
echo "<br>";
var_dump($model->load(Yii::$app->request->post()));
echo "To be continued...";
}
return $this->render('index',['model' => $model]);
}
}
然后是login/index.php的代码:
<?php
use yii\helpers\Html;
use Yii;
//http://www.yiiframework.com/doc-2.0/guide-output-client-scripts.html
$this->registerCssFile('@web/css/bslogin.css');
?>
<div class="container">
<?=Html::beginForm('','post',['class'=>'form-signin'])?>
<h2 class="form-signin-heading">Please sign in</h2>
<?=Html::activeInput('text',$model,'username',['id'=>'username','class'=>'form-control','placeholder'=>'用户名','autofocus'=>true])?>
<?=Html::error($model,'username')?>
<?=Html::activeInput('password', $model,'password',['id'=>'inputPassword','class'=>'form-control','placeholder'=>'密码'])?>
<?=Html::error($model,'password')?>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<?=Html::submitButton('登入',['class'=>'btn btn-lg btn-primary btn-block'])?>
<?=Html::endForm()?>
</div>
最后的就是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;
//登录返回用户信息来储存,用于后面的session,cookies等
public function rules(){
return [
[['username','password'],'required','message'=>'{attribute}必须要写入...'],
['username','validateUser'],
];
}
public function validateUser($attribute,$params){
$user = User::findOne(['username'=>$this->$attribute]);
if(!$user){
$this->addError($this->attribute,'貌似有错误了额...');
}
}
}
User Model的部分代码:
public function rules(){
return [
[['username','password','email'],'required','message'=>'{attribute}必须要填写哟,少年'],
['username','unique','message'=>'你落后了一步,{value}已经被占用'],
['email','email','message'=>'非邮箱地址'],
['password','string','min'=>6,'tooShort'=>'密码太短了...呵呵',],
['status','in','range'=>[0,1]],
];
}
清空用户输入后,提交信息,输出两个错误信息(username & password),然后测试只输入用户名不输入密码,还是同样的错误信息,好像用户名那里[输入=没输入]一样。或者全部输入正确用户名及密码,页面也不进行跳转,这是何故?
截图为输入用户命和密码后的输出(控制器添加了var_dump()):
【疑问】引用User模型的rules不会跟LoginForm的rules冲突吧?
brantyo 补充于 2016-08-10 10:27
这个是原版的LoginForm
<?php
namespace common\models;
use Yii;
use yii\base\Model;
/**
* Login form
*/
class LoginForm extends Model
{
public $username;
public $password;
public $rememberMe = true;
private $_user;
/**
* @inheritdoc
*/
public function rules()
{
return [
// username and password are both required
[['username', 'password'], 'required'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
// password is validated by validatePassword()
['password', 'validatePassword'],
];
}
/**
* Validates the password.
* This method serves as the inline validation for password.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
*/
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.');
}
}
}
/**
* Logs in a user using the provided username and password.
*
* @return boolean whether the user is logged in successfully
*/
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
} else {
return false;
}
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
protected function getUser()
{
if ($this->_user === null) {
$this->_user = User::findByUsername($this->username);
}
return $this->_user;
}
}
然后是controller的说
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
} else {
return $this->render('login', [
'model' => $model,
]);
}
}
比较困惑为什么这个可以成功,我的就不行。。
最佳答案
-
你的控制器
public function actionIndex(){ $model = new LoginForm(); if($model->load(Yii::$app->request->post() && $model->validate())){ return $this->redirect(['site/index']); }else{ var_dump(Yii::$app->request->isPost); echo "<br>"; var_dump($model->load(Yii::$app->request->post())); echo "To be continued..."; } return $this->render('index',['model' => $model]); }
你的loginForm里面没validate();
$model->validate()
怎么用他呢?还有你的loginForm继承model;那么不应该要指明表吗?,建议你继承user表;
至于你的疑问:继承user表,只要当前写了与被继承的模型一样的函数,只会覆盖,不会有冲突的。
共 4 条回复
其他 2 个回答
brantyo 加拿大
注册时间:2016-04-12
最后登录:2017-04-06
在线时长:2小时30分
最后登录:2017-04-06
在线时长:2小时30分
- 粉丝0
- 金钱105
- 威望0
- 积分125