Reputation: 1542
I began to learn cakephp 3 now, he's beta version so your manual is not complete yet..
I need to change the user's password, but it's triggered the Flash Error. Debugging the variable $user just show "The Field Required" but all table's fields are value NULL.
My Code:
//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 They are the field of user table.
[
'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 the patchEntity was created for you to use the existing entity, creating a new I would be creating a line at the database, then the solution was:
Just update UsersController, use a entity with the a user's id that 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');
}
Thx everybody ^^
Upvotes: 4
Views: 7244
Reputation: 60463
You are requiring these fields presence, so that's the expected behavior. As per your comments, setting the second argument of requirePresence()
to "false"
won't do anything, as that's a string, if you'd wanted to disable this check you'd have to supply a boolean value, ie false
without quotes. However this will just mess up your default validation.
That being said, there are various ways to solve the "problem" of fields not always being required, one would be to make use of a separate set of validation rules, something like:
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'
]);
See also
Upvotes: 5