CakePHP 3 - 更改密码

时间:2015-02-03 15:46:10

标签: php validation cakephp cakephp-3.0

我现在开始学习cakephp 3,他是测试版,所以你的手册尚未完成..

我需要更改用户的密码,但是它触发了Flash错误。调试变量$ user只显示“需要字段”,但所有表的字段都是NULL值。

我的代码:

 //Create a new Entity
 $user = $this->Users->newEntity();
                    // Set new password and user's id... I'm not doing with session yet, ok?
                    $user = $this->Users->patchEntity($user, ['password' => $this->request->data['new-password'], 'id' => 2]);
                    debug($user->errors());
                    if ($this->Users->save($user)) {
                        $this->Flash->success('Its Right');
                    } else {
                        $this->Flash->error('FAIL');
                    }

Debuggin $ user 它们是用户表的字段。

[
    'gym_id' => [
        (int) 0 => 'This field is required'
    ],
    'role_id' => [
        (int) 0 => 'This field is required'
    ],
    'name' => [
        (int) 0 => 'This field is required'
    ],
    'username' => [
        (int) 0 => 'This field is required'
    ],
    'stats' => [
        (int) 0 => 'This field is required'
    ]
]

我的 UserModel

    public function validationDefault(Validator $validator)
    {
        $validator
            ->add('id', 'valid', ['rule' => 'numeric'])
            ->allowEmpty('id', 'create')
            ->add('gym_id', 'valid', ['rule' => 'numeric'])
            ->requirePresence('gym_id', 'create')
            ->notEmpty('gym_id')
            ->add('role_id', 'valid', ['rule' => 'numeric'])
            ->requirePresence('role_id', 'create')
            ->notEmpty('role_id')
            ->requirePresence('name', 'create')
            ->notEmpty('name')
            ->requirePresence('username', 'create')
            ->notEmpty('username')
            ->requirePresence('password', 'create')
            ->notEmpty('password')
            ->add('stats', 'valid', ['rule' => 'numeric'])
            ->requirePresence('stats', 'create')
            ->notEmpty('stats');

        return $validator;
    }

已解决

根据文档,为您创建patchEntity以使用现有实体,创建一个新的我将在数据库中创建一行,然后解决方案是:

只需更新UsersController,使用具有您要更新的用户ID的实体

UsersController

$user_data = $this->Users
                        ->find()
                        ->where(['id' => 2])
                        ->first();

 $user = $this->Users->patchEntity($user_data, 
                        [
                        'password' => $this->request->data['new-password']                        
                        ]);
                    /*debug($user);
                    exit();*/
                    if ($this->Users->save($user)) {
                        $this->Flash->success('Success, GG EASY');
                    } else {
                        $this->Flash->error('FAIL, SurrenderAt20');
                    }

大家各位^^

1 个答案:

答案 0 :(得分:4)

您需要这些字段存在,因此这是预期的行为。根据您的评论,将requirePresence()的第二个参数设置为"false"将不会执行任何操作,因为这是一个字符串,如果您要禁用此检查,则必须提供一个布尔值,即false没有引号。然而,这只会搞乱您的默认验证。

话虽如此,有多种方法可以解决并非总是需要的字段的“问题”,一种方法是使用一组单独的验证规则,例如:

public function validationUpdatePassword(Validator $validator)
{
    $validator
        ->add('id', 'valid', ['rule' => 'numeric'])
        ->requirePresence('id')
        // you might want to add some actual password validation here
        ->requirePresence('password')
        ->notEmpty('password');

    return $validator;
}
$user = $this->Users->patchEntity($user, [
    'password' => $this->request->data['new-password'],
    'id' => 2
], [
    'validate' => 'updatePassword'
]);

另见