2010-09-21 38 views
7
var $validate = array(
    'password' => array(
     'passwordlength' => array('rule' => array('between', 8, 50),'message' => 'Enter 8-50 chars'), 
     'passwordequal' => array('checkpasswords','message' => 'Passwords dont match') 
) 
); 

function checkpasswords() 
{ 
    return strcmp($this->data['Airline']['password'],$this->data['Airline']['confirm password']); 
} 

Mã này không hoạt động và luôn đưa ra thông báo lỗi ngay cả khi chúng khớp nhau. Ngoài ra khi tôi thực hiện chỉnh sửa, tôi nhận được lỗi sau khi không có trường mật khẩu. có bất kỳ sửa chữa nào không?xác thực mật khẩu cakephp

Undefined index: password [APP/models/airline.php, line 25] 
+1

là '$ this-> datadata' dự định? Nếu không, có vấn đề của bạn. – Stephen

+0

tôi đã sửa mã trên để xóa dữ liệu thừa mà tôi vẫn gặp lỗi – aWebDeveloper

+0

Tôi có thể xem biểu mẫu html đăng dữ liệu không? – Stephen

Trả lời

5

đây là sai lầm

'passwordequal' => array('checkpasswords','message' => 'Passwords dont match') 

tôi đã thay đổi nó để

'passwordequal' => array('rule' =>'checkpasswords','message' => 'Passwords dont match') 

cũng hàm strcmp cũng có sai lầm vì nó sẽ trở về 0 (tức là False) tất cả các thời gian trong đoạn code trên

if(strcmp($this->data['Airline']['password'],$this->data['Airline']['confirm_password']) ==0) 
{ 
    return true; 
} 
return false; 
+5

Ồ, sự dư thừa khủng khiếp! Trong trường hợp này, bạn phải sử dụng 'return strcmp (...) == 0'. – deceze

12

Bạn đang sử dụng AuthComponent? Lưu ý rằng nó băm tất cả các trường mật khẩu đến (nhưng không phải là các trường "xác nhận mật khẩu", hãy kiểm tra với debug($this->data)), vì vậy các trường sẽ không bao giờ giống nhau. Read the manual and use AuthComponent::password để thực hiện kiểm tra.


Có nói rằng, đây là một cái gì đó tôi sử dụng:

public $validate = array(
    'password' => array(
     'confirm' => array(
      'rule' => array('password', 'password_control', 'confirm'), 
      'message' => 'Repeat password', 
      'last' => true 
     ), 
     'length' => array(
      'rule' => array('password', 'password_control', 'length'), 
      'message' => 'At least 6 characters' 
     ) 
    ), 
    'password_control' => array(
     'notempty' => array(
      'rule' => array('notEmpty'), 
      'allowEmpty' => false, 
      'message' => 'Repeat password' 
     ) 
    ) 
); 

public function password($data, $controlField, $test) { 
    if (!isset($this->data[$this->alias][$controlField])) { 
     trigger_error('Password control field not set.'); 
     return false; 
    } 

    $field = key($data); 
    $password = current($data); 
    $controlPassword = $this->data[$this->alias][$controlField]; 

    switch ($test) { 
     case 'confirm' : 
      if ($password !== Security::hash($controlPassword, null, true)) { 
       $this->invalidate($controlField, 'Repeat password'); 
       return false; 
      } 
      return true; 

     case 'length' : 
      return strlen($controlPassword) >= 6; 

     default : 
      trigger_error("Unknown password test '$test'."); 
    } 
} 

này là xấu vì những lý do sau đây:

  • Có khớp nối chặt chẽ với các hình thức, luôn luôn hy vọng một lĩnh vực password_control để có mặt. Bạn cần sử dụng danh sách trắng trường hoặc vô hiệu xác thực nếu bạn không có dữ liệu trong dữ liệu của mình, ví dụ: $this->User->save($this->data, true, array('field1', 'field2')).
  • Nhập mật khẩu theo cách thủ công của AuthComponent theo cách thủ công (vì không có quyền truy cập sạch vào các thành phần từ mô hình). Nếu bạn thay đổi thuật toán được sử dụng trong AuthComponent, bạn cũng cần thay đổi nó ở đây.

Có nói rằng, minh bạch xác nhận và tạo thông báo lỗi thích hợp cho cả trường mật khẩu và mật khẩu kiểm soát mà không yêu cầu bất kỳ mã bổ sung nào trong bộ điều khiển.

1

Heres là giải pháp của tôi:

Bạn phải để tạo ra một trận đấu phương thức có tên (Bạn có thể đặt tên cho nó những gì bạn thích):

public function match($check, $with) { 
    // Getting the keys of the parent field 
    foreach ($check as $k => $v) { 
     $$k = $v; 
    } 

    // Removing blank fields 
    $check = trim($$k); 
    $with = trim($this->data[$this->name][$with]); 

    // If both arent empty we compare and return true or false 
    if (!empty($check) && !empty($with)) { 
     return $check == $with; 
    } 

    // Return false, some fields is empty 
    return false; 
} 

Và phương pháp $ validate phải như thế này:

public $validate = array(
    'password' => array(
     'match' => array(
      'rule' => array('match', 'password2'), 
      'message' => 'Passwords doesnt match', 
     ), 
    ), 
); 

đâu password2 là lĩnh vực để so sánh lĩnh vực password đầu tiên bạn

Tôi rất vui được chia sẻ nó!: D

3

Đối Validate mật khẩu, mật khẩu cũ và xác nhận Password

class Adminpassword extends AppModel 
{ 


    public $name   = 'Admin'; 
      public $primaryKey = 'id'; 
      public $validate = array(
       'oldpassword' => array(
         array(
         'rule' => 'notEmpty', 
         'required' => true, 
         'message' => 'Please Enter Current password' 
         ), 
         array(
         'rule' =>'checkcurrentpasswords', 
         'message' => 'Current Password does not match' 
         ) 
       ), 
       'password' => array(
         array(
           'rule' => 'notEmpty', 
           'required' => true, 
           'message' => 'Please Enter password' 
         ), 
         array(        
         'rule' => array('minLength', 6), 
         'message' => 'Passwords must be at least 6 characters long.', 
         ) 
       ), 
       'cpassword' => array(
         array(
         'rule' => 'notEmpty', 
         'required' => true, 
         'message' => 'Please Enter Confirm password' 
         ), 
         array(
           'rule' => 'checkpasswords', 
           'required' => true, 
           'message' => 'Password & Confirm Password must be match.' 
         ) 
       ) 
      ); 

    function checkpasswords()  // to check pasword and confirm password 
    { 
     if(strcmp($this->data['Adminpassword']['password'],$this->data['Adminpassword']['cpassword']) == 0) 
     { 
      return true; 
     } 
     return false; 
    } 
    function checkcurrentpasswords() // to check current password 
    { 
     $this->id = $this->data['Adminpassword']['id']; 
     $user_data = $this->field('password');  
     //print_r(Security::hash($this->data['Adminpassword']['oldpassword'], 'sha1', true)); 
     if ($user_data == (Security::hash($this->data['Adminpassword']['oldpassword'], 'sha1', true))) 
     { 
      return true; 
     } 
     else 
     { 
     return false; 
     } 
    } 

} 
+0

Giải pháp này đã giúp tôi và rõ ràng – daniherculano

Các vấn đề liên quan