Working With Widgets

I would like my “login page” to be on my navbar. What I did was just copy what’s inside the actionLogin to actionIndex like so


	public function actionIndex()

	{

		$model=new LoginForm;


		// if it is ajax validation request

		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}


		// collect user input data

		if(isset($_POST['LoginForm']))

		{

			$model->attributes=$_POST['LoginForm'];

			// validate user input and redirect to the previous page if valid

			if($model->validate() && $model->login())

				$this->redirect(Yii::app()->createUrl('myprofile/index'));

		}

		$this->render('index');

	}

And my widget


<?php

    class LoginWidget extends CWidget

    {

        public function run() {

            $this->render('login-widget', array('model'=>new LoginForm()));

        }

    }

?>

The view


<?php         

    $form=$this->beginWidget('CActiveForm', array(

        'id'=>'login-form',

        'action'=>Yii::app()->createUrl('site/login'),

        'method'=>'POST',

        'enableAjaxValidation'=>true,

        'enableClientValidation'=>true,

        'clientOptions'=>array(

            'validateOnSubmit'=>true,

            )

        ));


    echo $form->errorSummary($model);

?>

    <div class="form-group">

        <?php echo $form->textField($model,'username',array('placeholder'=>'Employee Code','class'=>'form-control')); ?>

        <?php echo $form->error($model,'username'); ?>

    </div>

    <div class="form-group">

        <?php echo $form->passwordField($model,'password',array('placeholder'=>'Password','class'=>'form-control')); ?>

        <?php echo $form->error($model,'password'); ?>

    </div>

    <div class="form-group">

        <?php echo CHtml::submitButton('Sign in', array('class'=>'btn btn-success')); ?>

    </div>

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

But when I try to login, nothing happens. It just shows up on the browser what I just typed (which should not happen). I can only login thru site/login. You can try it at oamsystem2.cloudcontrolled.com

first try to debug what is received via POST array using


print_r($_POST['LoginForm']);

and if you get your username and password correctly then proceed

as i read source from the website you provided and in the form tag method not specified it might be the problem

I’m not sure what tag method you’re referring to. What do you suggest I do?

I’m afraid I’m not getting anything at all. What am I doing wrong?

in your navbar the form you rendered does not have the action example in <form> tag method is missing. so use like this




    $form=$this->beginWidget('CActiveForm', array(

        'id'=>'login-form',

        'action'=>Yii::app()->createUrl('site/login'),

        'enableAjaxValidation'=>true,

        'enableClientValidation'=>true,

        'clientOptions'=>array(

            'validateOnSubmit'=>true,

            )

        ));



Note:

[list=1]

[*]no need to specify action when you want to submit the form to the current action

[*]no need to specify method because it defaults to post

[/list]

you can read more about active form here