Stucked with Yii Framework Form tutorial-Collection of common programming errors
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(,,)
* 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
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
-
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.
- You are using model variable and passing loginModel variable.
- You are rendering view file on POST.
-
This is just a shot in the dark…
For your controller
public function actionLogin(){
addreturn $model;
at the end.Add 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.