Reputation: 19327
I keep on reading this tutorial of Yii framework over and over again Yii Framework - Working with Form
I already created my Model with this following codes
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",
);
}
}
and my Action function with this codes in my controller
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));
}
}
and lastly in my View
<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>
and i always came out with this error in my view D:\xampp\htdocs\wiltalk\protected\views\sandbox\index.php(11)
Undefined variable: model
do i miss something? please let me know... I know this is kinda simple but I am a first-timer in using such component-based MVC frameworks.... Thanks
Upvotes: 2
Views: 3947
Reputation: 3950
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));
}
Your code was not correct. Make these changes to your code.
Upvotes: 2
Reputation: 1950
This is just a shot in the dark...
For your controller public function actionLogin(){
add return $model;
at the end.
Add <?php $model = actionLogin(); ?>
to the top of your view.
The issue is that you're not setting $model
anywhere in your view, but your control is setting it. You must find some way to pass that $model
that's being set in your control back your your view.
Upvotes: 1