gpt4 book ai didi

php - 坚持使用 Yii 框架表单教程

转载 作者:行者123 更新时间:2023-12-04 05:55:53 28 4
gpt4 key购买 nike

我一直在一遍又一遍地阅读这篇 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",
);
}
}

以及我在 Controller 中使用此代码的 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\protected\views\sandbox\index.php(11)

Undefined variable: model



我想念什么吗?请让我知道...我知道这有点简单,但我是第一次使用这种基于组件的 MVC 框架....谢谢

最佳答案

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 上呈现 View 文件。
  • 关于php - 坚持使用 Yii 框架表单教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9527165/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com