我是Yii的新手。我正在为用户创建更改密码页面。当我得到验证时,我遇到了麻烦。
这是视图:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'changepassword-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'old_pwd'); ?>
<?php echo $form->textField($model,'old_pwd'); ?>
<?php echo $form->error($model,'old_pwd'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'pwd'); ?>
<?php echo $form->textField($model,'pwd'); ?>
<?php echo $form->error($model,'pwd'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'pwd_repeat'); ?>
<?php echo $form->passwordField($model, 'pwd_repeat'); ?>
<?php echo $form->error($model,'pwd_repeat'); ?>
<p class="hint">
Passwords must be minimum 6 characters and can contain alphabets, numbers and special characters.
</p>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Change Password'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
型号:
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('user, pwd, status, old_pwd, pwd_repeat', 'required'),
array('user', 'length', 'max'=>50),
array('pwd', 'length', 'max'=>30),
array('status', 'length', 'max'=>10),
array('pwd','compare'),
array('old_pwd','checkOldPassword'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, user, pwd, status', 'safe', 'on'=>'search'),
);
}
// FOR CHECKING IF THE PASSWORD IS VALID
public function checkOldPassword($attribute,$params)
{
$record=Admin::model()->findByAttributes(array('pwd'=>$this->attribute));
if($record===null){
$this->addError($attribute, 'Invalid password');
}
}
控制器:
/**
* Change Password for users
*/
public function actionChangePassword()
{
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
if(Yii::app()->user->isGuest){
// If the user is guest or not logged in redirect to the login form
$this->redirect(array('site/login'));
}
else{
$model = new Admin;
if(isset($_POST['Admin'])){
$model->attributes=$_POST['Admin'];
$this->render('changepassword',array('model'=>$model));
}
else{
$this->render('changepassword',array('model'=>$model));
}
}
}
我正在使用自定义验证程序“ checkOldPassword ”来检查用户的当前密码。现在的问题是自定义验证无效。此外,如果我关闭javascript 我发现服务器端验证错误也没有显示。我是Yii的新手。所以请原谅任何愚蠢的错误。 请帮忙。
答案 0 :(得分:2)
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
public $pwd_repeat;
public $old_pwd;
return array(
array('old_pwd,pwd_repeat,pwd','safe'),
array('user, pwd, status, old_pwd, pwd_repeat', 'required'),
array('user', 'length', 'max'=>50),
array('pwd', 'length', 'max'=>30),
array('status', 'length', 'max'=>10),
array('pwd','compare'),
array('old_pwd','checkOldPassword'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, user, pwd, status', 'safe', 'on'=>'search'),
);
}
// FOR CHECKING IF THE PASSWORD IS VALID
public function checkOldPassword($attribute,$params)
{
$record=Admin::model()->findByAttributes(array('pwd'=>$this->attributes['old_pwd']));
if($record===null){
$this->addError($attribute, 'Invalid password');
}
}
public function actionChangePassword()
{
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
if(Yii::app()->user->isGuest){
// If the user is guest or not logged in redirect to the login form
$this->redirect(array('site/login'));
}
else{
$model = new Admin;
if(isset($_POST['Admin'])){
$model->attributes=$_POST['Admin'];
$model->save();
$this->render('changepassword',array('model'=>$model));
}
else{
$this->render('changepassword',array('model'=>$model));
}
}
}
虽然你的方法还不好...... 但首先让它运作起来......
答案 1 :(得分:0)
试试这个:
// FOR CHECKING IF THE PASSWORD IS VALID
public function checkOldPassword($attribute,$params)
{
$record=Admin::model()->findByAttributes(array('pwd'=>$this->pwd));
if($record===null){
$this->addError($attribute, 'Invalid password');
}
}
将 $ this-&gt;属性替换为 $ this-&gt; pwd
答案 2 :(得分:0)
首先,我认为clientValidation不起作用,因为您正在检查旧密码,因此您需要服务器端验证(Ajax验证)。 clientValidation有助于检查密码长度等。
所以把这一行: 'enableAjaxValidation'=&GT;真,
其次,我会将您的代码更改为以下内容:
public function checkOldPassword($attribute,$params)
{
$record = $this->find(array(
'select' => 'old_password',
'condition' => 'username=:username',
'params' => array(':username' => $username//or user or w/e ur gonna find user),
));
if($record===null){
$this->addError($attribute, 'Invalid password');
}
else {
if($this->old_password != $record->old_password) {
$this->addError('old_password', "Invalid Password");
}
}
}