2016-05-02 7 views
0

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

Antwort

0

Sie haben Regeln für die Felder Validierungsdatei current_password, new_password und confirm_new_password, aber sie sind nur das Feld Passwort mit patchEntity Einstellung Deshalb ist diese Validatoren sind nicht genannt.

public function changePassword() 
{ 
    ... 
    $user = $this->Users->patchEntity($user, [ 
      'password'  => $this->request->data['new_password'], // There is no validator defined for the field 'password' 
     ], 
     ['validate' => 'password'] 
    ); 
    ... 
} 

In Ihrem Fall müssen Sie Ihren Code ändern mit

public function changePassword() 
{ 
    ... 
    $user = $this->Users->patchEntity($user, [ 
      'password'  => $this->request->data['new_password'], // Your actual field in db 
      'current_password' => $this->request->data['current_password'], // Trigger validation rule current_password 
      'new_password' => $this->request->data['new_password'], // Trigger validation rule new_password 
      'confirm_new_password' => $this->request->data['new_password'], // Trigger validation rule confirm_new_password 
     ], 
     ['validate' => 'password'] 
    ); 
    ... 
} 

Nach Ansicht Datei, die Sie haben auch das Unternehmen als erster Parameter von Form::create()

+0

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

+0

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

+0

Meinst du * die entsprechenden Felder *? Validierungsregeln werden erst in der Funktion patchEntity angewendet. Ich habe meine Antwort aktualisiert –

Verwandte Themen