Yii Framework Undefined Variable $model
===

I have a standard login form as follows:

    <?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'login-form',
	'enableClientValidation'=>true,
	'clientOptions'=>array(
		'validateOnSubmit'=>true,
	),
    )); ?>

	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<div class="row">
	 	<?php echo $form->labelEx($model,'username'); ?>
		<?php echo $form->textField($model,'username'); ?>
		<?php echo $form->error($model,'username'); ?>  
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'password'); ?>
		<?php echo $form->passwordField($model,'password'); ?>
		<?php echo $form->error($model,'password'); ?>
		
	</div>

	<div class="row rememberMe">
		<?php echo $form->checkBox($model,'rememberMe'); ?>
		<?php echo $form->label($model,'rememberMe'); ?>
		<?php echo $form->error($model,'rememberMe'); ?>
	</div>

	<div class="row buttons">
		<?php echo CHtml::submitButton('Login'); ?>
	</div>

    <?php $this->endWidget(); ?>

My UserIdentity class is:

    public function authenticate()
        {
    //                $username = $this->username;
    //                $password = $this->password;
               
                $user = Users::model()->findbyAttributes(array($username=>$this->username));
                if($user === NULL){
                        $this->errorCode=self::ERROR_UNKNOWN_IDENTITY;
                        
                        
                }else if ($user->password !== md5($this->password)){    
    //                        $this->username = $user->username;
    //                       sess('SESS_USER_INFO', $user->attributes);
    //                        $this->errorCode=self::ERROR_NONE;
			//invalid password
			$this->errorCode=self::ERROR_PASSWORD_INVALID;
                }
                else {
                	$this->errorCode=self::ERROR_NONE;
                }
                return !$this->errorCode;
        }
    }

My UserController.php actionLogin is as follow:

    public function actionLogin()
	{
		$model= new Users();
    //		Yii::app()->sessiion[username]= Yii::app()->model($username);
    //		Yii:app()->session[password] = Yii::app()->model($password);
		// if it is ajax validation request
		if(isset($_POST['ajax']))
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
    //                print_r($_POST['Users']);
		// collect user input data
		$form =  new LoginForm;
    //		if(isset($_POST['Users']))
    //		{
    //			$model->attributes=$_POST['Users'];     
                        
			// validate user input and redirect to the previous page if valid
    //			if($model->login())
    //				$this->redirect(Yii::app()->user->getReturnUrl('index'));
    //		}
		// display the login form
    //		$this->render('login',array('model'=>$model));
		if(isset($_POST['LoginForm'])){
		$form->attributes=$_POST['LoginForm'];
		if($form->validate() && $form->login()) $this->redirect(Yii::app()->user->returnUrl);
		}
		$this->render('login',array('model'=>$model));
	}


The LoginForm model is as follows:

    class LoginForm extends CFormModel
    {
	public $username;
	public $password;
	public $rememberMe;

	private $_identity;

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// username and password are required
			array('username, password', 'required'),
			// rememberMe needs to be a boolean
			array('rememberMe', 'boolean'),
			// password needs to be authenticated
			array('password', 'authenticate'),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
			'rememberMe'=>'Remember me next time',
		);
	}

	/**
	 * Authenticates the password.
	 * This is the 'authenticate' validator as declared in rules().
	 */
	public function authenticate($attribute,$params)
	{
		if(!$this->hasErrors())
		{
    //			$this->_identity=new UserIdentity($this->username,$this->password);
    //			if(!$this->_identity->authenticate())
    //				$this->addError('password','Incorrect username or password.');
			$identity = new UserIdentity($this->username, $this->password);
			$identity->authenticate();
			switch($identity->errorCode)
			{
				case UserIdentity::ERROR_NONE:
					$duration=$this->rememberMe ? 3600*24*30 : 0;  //30 days
					Yii::app()->user->login($identity,$duration);
					break;
				case UserIdentity::ERROR_USERNAME_INVALID:
					$this->addError('username', 'Username is incorrect.');
					break;
				default; // UserIdentity:ERROR_USERNAME_INVALID
					$this->addError('password','Password is incorrect.');
					break;
			}			
		}
	}

	/**
	 * Logs in the user using the given username and password in the model.
	 * @return boolean whether login is successful
	 */
	public function login()
	{
		if($this->_identity===null)
		{
			$this->_identity=new UserIdentity($this->username,$this->password);
			$this->_identity->authenticate();
		}
		if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
		{
			$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
			Yii::app()->user->login($this->_identity,$duration);
			return true;
		}
		else
			return false;
	}
    }

The issue we're having is that when we login using valid credenials on the home page, a new login form is loaded without indication that the user is authetnicated.  We are also seeing syntax errors stating the vaiable $model is undfined in the login form.  

How can I rectify the above issues?