view file login.php
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'userregistration-login-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'clientOptions'=>array('validateOnSubmit'=>true),
)); ?>
<div id="form1">
<div id="formDiv1">
<p style="margin: 0 0 20px 85px" class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo CHtml::$afterRequiredLabel = ' <span class="pink-color">*</span>'; echo $form->errorSummary($model); ?>
<div class="frmFields" style="margin: 0 0 0 40px;">
<?php echo $form->labelEx($model,'email', array("style"=>"color:#C29A4B; font-weight:bold")); ?>
<?php echo $form->emailField($model,'email', array("style"=>"border:1px solid #DCDCDD")); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="frmFields" style="margin: 0 0 0 40px;">
<?php echo $form->labelEx($model,'password', array("style"=>"color:#C29A4B; font-weight:bold")); ?>
<?php echo $form->passwordField($model,'password', array("style"=>"border:1px solid #DCDCDD")); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div id="frmBtnLink1">
<div style="float:left;">
<?php echo CHtml::submitButton('', array("id"=>"Login", "name"=>"Login", "style"=>"border-width:0px;")); ?>
</div>
<div style="float:left; margin-left:7px; padding-top:7px;"><a href="<?php echo Yii::app()->createUrl('site/registration'); ?>" style="color:#000000; text-decoration:underline; font-weight:bold;">Need an Account</a></div>
<div style="float:left; margin-left:7px; padding-top:7px;"><a href="javascript:forgotPassword();" style="color:#C29A4B; font-weight:bold; text-decoration:underline;" id="forgot">Forgot your password?</a></div>
</div>
<?php echo $form->hiddenField($model,'admin', array('value'=>'0')); ?>
</div>
</div>
<?php $this->endWidget(); ?>
model LoginForm.php
class LoginForm extends CFormModel
{
public $email;
public $password;
private $_identity;
public function rules()
{
return array(
array('email, password', 'required'),
array('email', 'email'),
array('password', 'authenticate'),
);
}
/**
* Declares attribute labels.
*/
public function attributeLabels()
{
return array('email'=>'User Name/Email Address'
);
}
public function authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$identity=new UserIdentity($this->email,$this->password);
$identity->authenticate();
switch($identity->errorCode)
{
case UserIdentity::ERROR_NONE:
Yii::app()->user->login($identity);
break;
case UserIdentity::ERROR_USERNAME_INVALID:
$this->addError('email','Email address is incorrect.');
break;
default: // UserIdentity::ERROR_PASSWORD_INVALID
$this->addError('password','Password is incorrect.');
break;
}
}
}
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->email,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
Yii::app()->user->login($this->_identity,0);
return true;
}
else
return false;
}
}
SiteController login action
public function actionLogin()
{
if(isset($_SESSION["Search"]) && $_SESSION["Search"]!= null){
$this->redirect(array($_SESSION["Search"]));
}
$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()->user->returnUrl);
}
// display the login form
$this->render('login',array('model'=>$model));
}
Whenever I fill the email and password fields and submit I get this error: User Name/Email Address cannot be blank.