CakePHP 3 - Change Password

I started learning cakephp 3 now, it is in beta, so your tutorial is not complete yet.

I need to change the user password, but it caused a Flash error. Debugging the $ user variable just shows a "Required field", but all the fields in the table are NULL.

My code is:

 //Create a new Entity
 $user = $this->Users->newEntity();
                    // Set new password and user 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 . This is a user table field.

[
    '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'
    ]
]

My 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;
    }

SOLVED

According to the documentation, patchEntity was created for you to use an existing object, creating a new one, I would create a row in the database, then the solution was:

Just update the UsersController, use the entity with the identifier of the user you want to update

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');
                    }

thanks everyone ^^

+4
1

, . 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'
]);

.

+4

All Articles