Hi friends
Only on my login-form client side validation doesn’t work (Also ajax validation doesn’t work).
Do you encountered with this problem?
[b]
Action:[/b]
public function actionSignin()
{
$model=new LoginForm;
/*if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}*/
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 == 'index.php') ? Yii::app()->baseUrl : Yii::app()->user->returnUrl);
//Yii::app()->request->redirect(Yii::app()->user->returnUrl);
$this->redirect($this->createUrl('/'));
}
$this->render('login',array('model'=>$model));
}
Model:
<?php
/**
* LoginForm class.
* LoginForm is the data structure for keeping
* user login form data. It is used by the 'login' action of 'SiteController'.
*/
class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe;
public $status;
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', 'message'=>'*'),
array('username, password', 'length'),
// 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(
'username'=>Yii::t('model','Username'),
'password'=>Yii::t('model','Password'),
'rememberMe'=>Yii::t('model','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() === UserIdentity::ERROR_PASSWORD_INVALID
or $this->_identity->authenticate() === UserIdentity::ERROR_USERNAME_INVALID)
$this->addError('password',Yii::t('model','Incorrect username or password'));
if($this->_identity->authenticate() === UserIdentity::ERROR_USER_INACTIVE)
$this->addError('status',Yii::t('model','Your account is inactive.'));
if($this->_identity->authenticate() === UserIdentity::ERROR_USER_BLOCKED)
$this->addError('status',Yii::t('model','Your account is blocked.'));
}
}
/**
* 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;
}
}
View:
<?php
/* @var $this SiteController */
/* @var $model LoginForm */
/* @var $form CActiveForm */
$this->pageTitle=Yii::app()->name . ' - Login';
?>
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
/*'enableAjaxValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),*/
)); ?>
<?php if($model->hasErrors('status')){ ?>
<div class="flash-notice">
<?php echo $form->error($model,'status');} ?>
</div>
<div class="form wide" id="login-form">
<div class="row">
<?php echo $form->label($model,'username'); ?>
<?php echo $form->textField($model,'username'); ?>
<?php echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->label($model,'password'); ?>
<?php echo $form->passwordField($model,'password'); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div class="row rememberMe">
<?php echo $form->label($model,'rememberMe'); ?>
<?php echo $form->checkBox($model,'rememberMe'); ?>
<?php echo $form->error($model,'rememberMe'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton(Yii::t('form','Signin'),array('id'=>'signin')); ?>
<?php echo CHtml::link(Yii::t('form','Forget password'),$this->createUrl('forgetPass',array(
'step'=>'step1')),array('id'=>'forgetPassword')); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
[/code]