有关Yii框架表单验证的有关问题。懂Yii的大神请进来看看吧.

WBOY
發布: 2016-06-13 11:59:31
原創
904 人瀏覽過

有关Yii框架表单验证的问题。懂Yii的大神请进来看看吧...
我在用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'])){ 吧
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!