有關Yii架構表單驗證的問題。懂Yii的大神請進來看看吧.

來源:互聯網
上載者:User
我在用Yii做一個簡單的小blog,在做後台登陸驗證的時候,出現bug,
不提示登陸錯誤資訊,我做的是帳號,密碼,驗證碼不可為空。
而且也不與資料庫匹配。我已經測試過,資料庫連接完全沒有問題。

LoginController.php
/**
* 後台登陸控制器
*/
class LoginController extends Controller{
/**
* 後台登陸模板
*/
public function actionIndex(){
$loginForm = new LoginForm();
if(isset($_POST['$LoginForm'])){
$loginForm->attributes = $_POST['LoginForm'];
if($loginForm->validate()){
echo 1;die;
}
}
$this->render('index',array('loginForm' => $loginForm));
}

public function actions(){
return array(
'captcha' => array(
'class' => 'system.web.widgets.captcha.CCaptchaAction',
'height' => 25,
'width' => 80,
'minLength' => 4,
'maxLength' => 4,
'offset' => 3,
'padding' => 1
),
);
}
}

這個是model,User.php
/**
* 後台使用者模型
*/
class User extends CActiveRecord{
/**
* 必不可缺的方法一,返回模型
*/
public static function model($className = __CLASS__){
return parent::model($className);
}

/**
* 必不可卻的方法二,返回表名
*/
public function tableName(){
return "{{user}}";
}
}

這個是LoginForm.php中的一個方法
public function rules()
{
return array(
// username and password are required
array('username', 'required', 'message' => '使用者名稱不可為空'),
array('password', 'required', 'message' => '密碼不可為空'),
// rememberMe needs to be a boolean
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('password', 'authenticate'),
//自訂錯誤資訊
array('captcha', 'captcha', 'message' => '驗證碼錯誤'),
);
}

這個是輸出錯誤資訊的頁面 index.php

  • error($loginForm,'username') ?>



  • error($loginForm,'password') ?>



  • error($loginForm,'captcha') ?>



這個是components目錄中的 UserIdentity.php

/**
* UserIdentity represents the data needed to identity a user.
* It contains the authentication method that checks if the provided
* data can identity the user.
*/
class UserIdentity extends CUserIdentity
{
/**
* Authenticates a user.
* The example implementation makes sure if the username and password
* are both 'demo'.
* In practical applications, this should be changed to authenticate
* against some persistent user identity storage (e.g. database).
* @return boolean whether authentication succeeds.
*/
public function authenticate()
{
$userInfo = User::model()->find('username = :name', array(':name' => $this->username));
if($userInfo == NULL){
$this->errorCode = self::ERROR_USERNAME_INVALID;
return false;
}
if($userInfo->password !== md5($this->password)){
$this->errorCode = self::ERROR_PASSWORD_INVALID;
return false;
}
$this->errorCode = self::ERROR_NONE;
return true;
}
}


到底哪裡出錯了,資料庫連結已經測過,完全沒有問題。
現在的情況是,即使寫了錯誤資訊,也不提示,也不執行登陸驗證。除了驗證碼可以更換之外,完全就像一個靜態頁面。


回複討論(解決方案)

if(isset($_POST['$LoginForm'])){ ???
是 if(isset($_POST['LoginForm'])){ 吧

if(isset($_POST['$LoginForm'])){ ???
是 if(isset($_POST['LoginForm'])){ 吧




對對對,沒錯的。謝謝,謝謝...
我都不知道那個$是怎麼寫上去的...
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.