Question

i am beginner in yii . i wanna use Chtml in view and use Cactiverecord in model for authentication but is doesn't work and show me error for correct email and pass

my view code is: login_form.php

<div class="form">
<?php echo Yii::app()->user->getFlash('notification');?>
<?php echo CHtml::beginForm(); ?>

<?php echo CHtml::errorSummary($model); ?>

<div class="row">
    <?php echo CHtml::activeLabel($model,'user_email'); ?>
    <?php echo CHtml::activeTextField($model,'user_email') ?>
</div>

    <div class="row">
    <?php echo CHtml::activeLabel($model,'user_password'); ?>
    <?php echo CHtml::activePasswordField($model,'user_password') ?>
</div>

<div class="row">
    <?php echo CHtml::activeLabel($model,'rememberMe'); ?>
    <?php echo CHtml::activeCheckBox($model,'rememberMe') ?>
</div>

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

<?php echo CHtml::endForm(); ?>
</div><!-- form -->

and my controller is:

public function actionLogin_form()
{
    $model=new Client;

    // 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['Client']))
    {
        $model->attributes=$_POST['Client'];
        // validate user input and redirect to the previous page if valid
        if($model->validate() && $model->login())  //&& $model->client()   && $model->login()
        {
            print_r($_REQUEST);
            //echo Yii::app()->user->isGuest;
            //return;
            //$this->redirect('Yii::app()->createAbsoluteUrl'); //Yii::app()->user->returnUrl
        }else echo "NO_";

    }
    // display the login form
    $this->render('login_form',array('model'=>$model));
}

in model: Client.php

<?php

class Client extends CActiveRecord
{
public $user_email;
public $user_password;
public $rememberMe;

private $_identity;

    public function tableName()
{
    return 'user';
}

    public function rules()
{
    return array(
        array('user_email','authenticate'),
        //array('username','password', 'length', 'max'=>30),    ,'user_password'
        //array('user_email','unique'),
        array('rememberMe', 'boolean'),
        // password needs to be authenticated
        array('user_password', 'authenticate'),
                );
}

public function attributeLabels()
{
    return array(
        'user_id' => 'id',
        'user_email' => 'email',
        'user_password' => 'password',
        'rememberMe'=>'Remember me next time',
                );
 }


    public function authenticate($attribute,$params)
{
    if(!$this->hasErrors())
    {
        $this->_identity=new UserIdentity($this->user_email,$this->user_password);
        if(!$this->_identity->authenticate())
            $this->addError('user_password','Incorrect email or password.');
            $this->addError('user_email','Incorrect email or password.___');
    }
}


    public function login()
{
    if($this->_identity===null)
    {
        $this->_identity=new UserIdentity($this->user_email,$this->user_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;
}


    public static function model($className=__CLASS__)
  {
    return parent::model($className);
  }


  }#end of class


 ?>   

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top