更新时未发生任何模型验证。 $model->validate()
始终返回true。因此即使错误的数据也会发生保存
以下是更改密码功能
查看
<?php echo $form->passwordField($model, 'currentpassword', array('class'=>'form-control','required'=>'required', 'value'=>'', 'maxlength'=>'40', 'pattern'=>'[a-zA-Z0-9-]{6,40}', 'title'=>'Password should be 6-40 characters containing a-z and 0-9')); ?>
<?php echo $form->error($model, 'currentpassword'); ?>
<?php echo $form->passwordField($model, 'password', array('class'=>'form-control','required'=>'required', 'value'=>'', 'maxlength'=>'40', 'pattern'=>'[a-zA-Z0-9-]{6,40}', 'title'=>'Password should be 6-40 characters containing a-z and 0-9')); ?>
<?php echo $form->error($model, 'password'); ?>
<?php echo $form->passwordField($model, 'confirmpassword', array('class'=>'form-control','required'=>'required', 'value'=>'', 'maxlength'=>'40', 'pattern'=>'[a-zA-Z0-9-]{6,40}', 'title'=>'Password should be 6-40 characters containing a-z and 0-9')); ?>
<?php echo $form->error($model, 'confirmpassword'); ?>
控制器
public function actionChangepassword()
{
$this->layout = (Yii::app()->request->isAjaxRequest) ? '//layouts/ajax' : '//layouts/precolumn2';
$model = new User('changepassword');
$data = array();
if (isset($_POST['User'])) {
$model = User::model()->findByPk(Yii::app()->User->getId());
$model->attributes=$_POST['User'];
if ($model->save()) {
$message = array(
'type' =>'success',
'message' =>'Password Changed.');
$data['message'] = $message;
}
}
$data['model'] = $model;
$this->render('changepassword',$data);
}
模型
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('name, user_type_id', 'required','on'=>'signup'),
array('name, email', 'length', 'max'=>255),
array('email', 'required','on'=>array('recover','signup')),
array('email', 'exists','on'=> 'recover'),
array('email', 'unique'),
array('email', 'email'),
array('user_login_count, user_like_count, user_share_count, user_view_count, user_comment_count, user_rating_count', 'numerical', 'integerOnly'=>true),
array('password', 'length', 'max'=>100),
array('password, confirmpassword', 'required','on'=>array('signup','resetpassword','changepassword')),
array('confirmpassword', 'compare', 'compareAttribute'=>'password','on'=>array('signup','resetpassword','changepassword'),'message'=>'Passwords dont match'),
array('currentpassword', 'compareCurrentPassword','on'=>array('changepassword')),
);
}
答案 0 :(得分:1)
查看您的专线$model = User::model()->findByPk(Yii::app()->User->getId());
您似乎需要在那里设置方案,因为您在没有“更改密码”的情况下创建了新用户。场景。
例如:
$model = User::model('changepassword')->findByPk(Yii::app()->User->getId());