Ich erstelle ein Passwort ändern Formular in CakePHP, die erfordert, dass ein Benutzer sein/ihr aktuelles Passwort, neues Passwort eingeben und neues Passwort bestätigen. Nach der Eingabe wird das Passwort des Benutzers geändert, da alle Einträge den festgelegten Validierungsregeln folgen.CakePHP Validator Regeln in UsersTable.php funktioniert nicht
Die Validierungsregeln funktionieren jedoch nicht. Der Benutzer muss nicht alle Felder ausfüllen, das neue Passwort auf eine bestimmte Länge setzen, sicherstellen, dass das neue Passwort und das Passwort übereinstimmen, usw. Ich kann nicht herausfinden, was das Problem ist.
Ich werde alle relevanten Code unten zur Verfügung stellen. Thanks :)
change_password.ctp (Die View-Datei)
<div class="users form large-9 medium-9 columns">
<?= $this->form->create() ?>
<fieldset>
<legend> <?= __('Change Password') ?> </legend>
<?= $this->Form->input('current_password', ['type' => 'password']) ?>
<?= $this->Form->input('new_password', ['type' => 'password']) ?>
<?= $this->Form->input('confirm_new_password', ['type' => 'password']) ?>
</fieldset>
<?= $this->Form->button(__('Save')) ?>
<?= $this->Form->end() ?>
</div>
change() in Userscontroller
public function changePassword()
{
$user = $this->Users->get($this->Auth->user('id'));
if (!empty($this->request->data)) {
$user = $this->Users->patchEntity($user, [
'password' => $this->request->data['new_password'],
],
['validate' => 'password']
);
if ($this->Users->save($user)) {
$this->Flash->success('The password is successfully changed');
$this->redirect('/users');
} else {
$this->Flash->error('There was an error during the save!');
}
}
$this->set('user', $user);
}
validationPassword() in UsersTable (dh die Validierungsregeln)
public function validationPassword(Validator $validator)
{
$validator
->add('current_password','custom',[
'rule'=> function($value, $context){
$user = $this->get($context['data']['id']);
if ($user) {
if ((new DefaultPasswordHasher)->check($value, $user->password)) {
return true;
}
}
return false;
},
'message'=>'Incorrect Password!',
])
->notEmpty('current_password');
$validator
->add('new_password', [
'length' => [
'rule' => ['minLength', 6],
'message' => 'The password must be at least 6 characters!',
]
])
->add('new_password',[
'match'=>[
'rule'=> ['compareWith','confirm_new_password'],
'message'=>'The passwords does not match!',
]
])
->notEmpty('new_password');
$validator
->add('confirm_new_password', [
'length' => [
'rule' => ['minLength', 6],
'message' => 'The password must be at least 6 characters!',
]
])
->add('confirm_new_password',[
'match'=>[
'rule'=> ['compareWith','new_password'],
'message'=>'The passwords does not match!',
]
])
->notEmpty('confirm_new_password');
return $validator;
}
EDIT: Added User.php
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\Validation\Validator;
/**
* User Entity.
*
* @property int $id
* @property string $first_name
* @property string $last_name
* @property string $email
* @property string $username
* @property string $password
*/
class User extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* @var array
*/
protected $_accessible = [
'*' => true,
'id' => false,
];
/**
* Fields that are excluded from JSON an array versions of the entity.
*
* @var array
*/
protected $_hidden = [
'password'
];
protected function _setPassword($value) {
$hasher = new DefaultPasswordHasher();
return $hasher->hash($value);
}
}
Meine Benutzertabelle passieren vergessen hat eine Spalte für Passwort, das das Passwort des Benutzers enthält. Die anderen Felder sind für die Validierung vorgesehen, werden aber nicht in die Benutzertabelle eingefügt. Also, wenn ich den Code hinzufügen würde, um die anderen Felder zu setzen, würde ich nicht in der Lage sein, in meine DB einzufügen. – skipper
Ich habe versucht, zu tun, was Sie empfohlen haben, und es gibt mir einen Fehler. Weil ich nicht die entsprechenden Tabellen für new_password, current_password und confirm_new_password habe – skipper
Meinst du * die entsprechenden Felder *? Validierungsregeln werden erst in der Funktion patchEntity angewendet. Ich habe meine Antwort aktualisiert –