在YII模型中使用确认密码(比较规则)时无法更新数据

时间:2013-01-02 04:53:25

标签: yii

在注册表中,使用密码字段和确认密码字段。使用此数据后无法更新。 模型

public function rules()
    {
            // NOTE: you should only define rules for those attributes that
            return array(
                    array('name, password', 'required'),
                    array('password', 'compare', 'compareAttribute'=>'confirm_password'), 
                    array('name', 'length', 'max'=>55),
                    // The following rule is used by search().
                    // Please remove those attributes that should not be searched.
                    array('id, name', 'safe', 'on'=>'search'),
            );
    }

尝试从index.php更新用户模型

`$post= User::model()->findByPk(1); $post->name='Abcdef'; $post->password='newpassword'; $post->save();`

新数据未更新?什么时候解决?

2 个答案:

答案 0 :(得分:1)

更新无效,因为尚未设置confirmpassword。如果更新时不需要密码,则密码为scenario,否则将始终检查密码。

     public function rules()
     {
        // NOTE: you should only define rules for those attributes that
        return array(
                array('name', 'required'),
                array('password', 'required','on'=>'create'),
                array('password', 'compare', 'compareAttribute'=>'confirmpassword','on'=>'create'),
                array('name', 'length', 'max'=>55),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('id, name', 'safe', 'on'=>'search'),
        );

答案 1 :(得分:-1)

    public $confirmpassword;

    public function rules()
    {
            // NOTE: you should only define rules for those attributes that
            return array(
                    array('name, password, confirmpassword', 'required'),
                    array('password', 'compare', 'compareAttribute'=>'confirmpassword'), 
                    array('name', 'length', 'max'=>55),
                    // The following rule is used by search().
                    // Please remove those attributes that should not be searched.
                    array('id, name', 'safe', 'on'=>'search'),
            );
    }