Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 学习Yii框架表单教程_Php_Forms_Yii - Fatal编程技术网

Php 学习Yii框架表单教程

Php 学习Yii框架表单教程,php,forms,yii,Php,Forms,Yii,我一遍又一遍地阅读Yii框架的教程 我已经用以下代码创建了我的模型 class LoginForm extends CFormModel{ public $username; public $password; public $rememberMe = false; private $_identity; public function rules(){ return array( /* array(<f

我一遍又一遍地阅读Yii框架的教程

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

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",
        );
    }
}
最后,我认为

<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\protected\views\sandbox\index.php(11)

未定义变量:模型


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

这只是在黑暗中拍摄…

对于您的控制器,
public function actionLogin(){
add
return$model;
在末尾。
添加到视图顶部。

问题是您没有在视图中的任何位置设置
$model
,但控件正在设置它。您必须找到某种方法将控件中设置的
$model
传递回视图

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));
    }
您的代码不正确。 对代码进行这些更改

  • 您正在使用模型变量并传递loginModel变量
  • 您正在POST上渲染视图文件

  • 第10行返回错误“致命错误:调用D:\xampp\htdocs\wiltalk\protected\views\sandbox\index.php中未定义的函数actionLogin()”查看本教程…我没有看到任何变量“$model”已在视图中传递,我已经得到了,先生^ ^ ^模型中没有问题…但是在控制器中…我无法使用我的控制器将模型实例传递到视图,无论如何,感谢^ ^ ^ ^更改此-$this->render('login',array('loginModel'=>$model));改为此-$this->render('login',array('model'=>$model));
    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));
        }