被Yii框架表格教程所困

时间:2012-03-02 02:27:41

标签: php forms yii

我一遍又一遍地阅读Yii框架的这个教程 Yii Framework - Working with Form

我已使用以下代码创建了我的模型

class LoginForm extends CFormModel{
    public $username;
    public $password;
    public $rememberMe = false;

    private $_identity;

    public function rules(){
        return array(
            /* array(<field>,<field>,<function to invoke>)
            * functions required and boolean are built-in validators of the yii framework.
            * you can invoke your own function by defining your own function
            */
            array('username','password','required'),
            array('rememberMe','boolean'),
            array('password','authenticate'),
        );
    }

    public function authenticate(){
        $this->_identity = new UserIdentity($this->username,$this->password);
        if(!$this->_identity->authenticate()){
            $this->addError("password","Incorrect Username or Password");
        }
    }

    public function attributeLabels(){
        return array(
            'username'=>"Username",
            'password'=>"Password",
            'rememberMe'=>"Remember Me",
        );
    }
}

和我的Action函数在我的控制器中使用此代码

 public function actionLogin(){
        //calls the Login Model that will be used in this action
        $model = new LoginForm;
        if(isset($_POST["LoginForm"])){
            //collects user input
            $model->attributes = $_POST["LoginForm"];
            //validates user input using the model rules and redirects back to
            //previous page when user input is invalid
            if($model->validate()){
               $this->redirect(Yii::app()->user->returnUrl);   
            }
            //redisplay the login form
            $this->render('login',array('loginModel'=>$model));
        }
    }

最后在我的视图中

<div class="form">
<?php
    $formlogin = $this->beginWidget('CActiveForm');     
    echo $formlogin->errorSummary($model);
?>
       <div class="row">
       <?php
            $formlogin->label($model,'username');
            $formlogin->textField($model,'username');
       ?>
       </div>
       <div class="row">
       <?php
            $formlogin->label($model,'password');
            $formlogin->passwordField($model,'password');
       ?>
       </div>
       <div class="row rememberMe">
       <?php
            $formlogin->checkBox($model,'rememberMe');
            $formlogin->label($model,'rememberMe');
       ?>
       </div>
       <div class="row submit">
       <?php
            echo CHtml::submitButton('Login');
       ?>
       </div>
<?php 
$this->endWidget();
?>
</div>

我总是在我看来出现这个错误 d:\ XAMPP \ htdocs中\ wiltalk \保护\视图\沙箱\的index.php(11)

  

未定义的变量:模型

我错过了什么吗?请让我知道......我知道这有点简单,但我是使用这种基于组件的MVC框架的第一个定时器....谢谢

2 个答案:

答案 0 :(得分:2)

public function actionLogin(){
        //calls the Login Model that will be used in this action
        $model = new LoginForm;
        if(isset($_POST["LoginForm"])){
            //collects user input
            $model->attributes = $_POST["LoginForm"];
            //validates user input using the model rules and redirects back to
            //previous page when user input is invalid
            if($model->validate()){
               $this->redirect(Yii::app()->user->returnUrl);   
            }            
        }
        //redisplay the login form
        $this->render('login',array('model'=>$model));
    }

您的代码不正确。 对代码进行这些更改。

  1. 您正在使用模型变量并传递loginModel变量。
  2. 您正在POST上呈现视图文件。

答案 1 :(得分:1)

这只是黑暗中的一击......

对于您的控制器public function actionLogin(){,最后添加return $model;<?php $model = actionLogin(); ?>添加到视图顶部。

问题是您没有在视图中的任何位置设置$model,但您的控件是设置它。你必须找到一些方法来传递你在控制中设置的$model你的观点。