zhong21jia 2012-01-02 11:53:00 2648次浏览 2条回复 0 0 0

模型:

array('username, verifyPassword, password, email, createtime, lastvisit, superuser, status', 'required'),
array('verifyPassword', 'compare', 'compareAttribute'=>'password', 'message' => UserModule::t("Retype Password is incorrect.")),[/code]视图:[code]    <div class="row">
<?php echo CHtml::activeLabelEx($model, 'verifyPassword'); ?>
<?php echo CHtml::activePasswordField($model, 'verifyPassword'); ?>
<?php echo CHtml::error($model, 'verifyPassword'); ?>
</div>

控制器:

if ($model->validate() && $profile->validate()) {
    $model->password = UserModule::encrypting($model->password);
    var_dump($model->attributes);
    var_dump($model->save());
    //exit;
    if ($model->save()) {
        $profile->user_id = $model->id;
        $team->parent_id = YII::app()->user->id;
        $profile->save();
        $team->save(); //保存用户信息的同时保存会员推荐信息。
    }

结果:

array
  'activkey' => string 'b198f491f667ecd25192321406ae7cd3' (length=32)
  'createtime' => int 1325476377
  'lastvisit' => int 1325476377
  'superuser' => string '0' (length=1)
  'status' => string '1' (length=1)
  'username' => string 'dddddd' (length=6)
  'password' => string '50f84daf3a6dfd6a9f20c9f8ef428942' (length=32)
  'email' => string 'dddddddd111@11.com' (length=18)
  'id' => null

boolean false

保存失败!

如果我在模型中不加

array('verifyPassword', 'compare', 'compareAttribute'=>'password', 'message' => UserModule::t("Retype Password is incorrect."))

这个规则。保存成功! 求大侠解惑!说一说可能是哪方面出了问题!

  • 回复于 2012-01-02 11:54 举报

    挽尊......

  • 回复于 2012-01-02 12:46 举报

    答案找到了!
    因为模型在保存动作执行的时候会再次进行验证!而保存之前,密码是经过了加密的!而确认密码却没有加密过,所以会出现保存不成功!
    只要在保存前加一段加密的代码就行了!如下:

    if ($model->validate() && $profile->validate()) {
        $model->password = UserModule::encrypting($model->password);
        $model->verifyPassword=UserModule::encrypting($model->verifyPassword);
    }
    
您需要登录后才可以回复。登录 | 立即注册