我一遍又一遍地阅读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框架的第一个定时器....谢谢未定义的变量:模型
答案 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 :(得分:1)
这只是黑暗中的一击......
对于您的控制器public function actionLogin(){
,最后添加return $model;
将<?php $model = actionLogin(); ?>
添加到视图顶部。
问题是您没有在视图中的任何位置设置$model
,但您的控件是设置它。你必须找到一些方法来传递你在控制中设置的$model
你的观点。