2013-08-16 2 views
0

암호 확인을 위해 this post을 추적했지만 CakePHP가 내 re_password 유효성 검사 설정을 건너 뛰었습니다.입력에 대한 유효성 검사 건너 뛰기 CakePHP

여기이 내 사용자 모델에 내 양식

<?php echo $this->Form->create('User'); ?> 
    <fieldset> 
     <legend><?php echo __('Add Account'); ?></legend> 
    <?php 
     echo $this->Form->input('username'); 
     echo $this->Form->input('email'); 
     echo $this->Form->input('password'); 
     echo $this->Form->input('re_password', array('type'=>'password', 'label'=>'Re-Enter Password', 'value'=>'')); 
     echo $this->Form->input('role', array('type' => 'hidden', 'default' => 'user')); 
    ?> 
    </fieldset> 
<?php echo $this->Form->end(__('Submit')); ?> 

function equalToField($array, $field) { 
    return strcmp($this->data[$this->alias][key($array)], $this->data[$this->alias][$field]) == 0; 
} 

public $validate = array(
    'username' => array(
     'required' => array(
      'rule' => array('minLength', '3'), 
      'message' => 'A username with a minimum length of 3 characters is required' 
     ), 
     'unique' => array(
      'rule' => 'isUnique', 
      'message' => 'This username has already been taken.' 
     ) 
    ), 
    'email' => array(
     'email' => array(
     'rule' => array('email'), 
     'message' => 'Please enter a valid email address.', 
     ) 
    ), 
    'password' => array(
     'required' => array(
      'rule' => array('minLength', '8'), 
      'message' => 'A password with a minimum length of 8 characters is required' 
     ) 
    ), 
    're_password' => array(
     'required' => array(
      'rule' => array('equalToField', 'password'), 
      'message' => 'Passwords do not match' 
     ) 
    ) 
); 

MINLENGTH 규칙이 암호 필드에 트리거 된 경우 오류가 발생하지만 아무것도 re_password 필드 ocurrs하지

무슨 일이 일어날 지 알기 위해 equalToField 메소드를 삭제했습니다. 심지어 오류가 발생하지 않았으므로 re_password가 보이지 않는 것처럼 보입니다.

이 작업과 관련이 있는지 잘 모르겠지만 re_password와 관련된 암호 필드에 추가 규칙을 추가하면 "Undefined index : re_password [APP/Model/User. php "

'password' => array(
     'required' => array(
      'rule' => array('minLength', '8'), 
      'rule' => array('equalToField', 're_password'), 
      'message' => 'A password with a minimum length of 8 characters is required' 
     ) 
    ), 

또한 내 UsersController 조치에서 인쇄 된 (this-> request-> data) 및 re_password 필드가 설정되었습니다.

+4

당신은 당신의 컨트롤러 코드를 게시하지 않았다. 문제의 출처가 아마도 있습니다. 추신 : 당신은 항상 당신이 사용하고있는 정확한 cakephp 버전을 언급해야합니다. – mark

+0

@mark 컨트롤러를 살펴볼 것을 제안 해 주셔서 감사합니다. 이전에 암호를 해시 한 다음 저장했음을 잊었습니다. 내가 그것을 제거하고 이제 유효성 검사가 제대로 작동합니다. – user2443591

+1

@mark에 +35의 가상 명성! –

답변

0

CakePHP2 위해 당신은 다음과 같이 사용할 수 있습니다 : 이것은 나를 위해 노력하고 있습니다

public $validate = array(
    'password' => array(
     'length' => array(
      'rule' => array('minLength', '8'), 
      'message' => 'Password should have at least 8 chars.' 
     ) 
    ), 
    'password_confirmation' => array(
     'length' => array(
      'rule' => array('minLength', '8'), 
      'message' => 'Password should have at least 8 chars.' 
     ), 
     'compare' => array(
      'rule' => array('validate_passwords'), 
      'message' => 'Passwords are not same.', 
     ) 
    ), 
); 

public function validate_passwords() { 
    return $this->data[$this->alias]['password'] === $this->data[$this->alias]['password_confirmation']; 
} 
0

이와 시도를 ..

public $validate = array(
      'password' => array(
       'minLength' => array(
        'rule' => array('minLength', 4), 
        'message' => 'Password must be at least 4 characters long' 
       ), 
       'maxLength' => array(
        'rule' => array('maxLength', 50), 
        'message' => 'Password cannot be longer than 50 characters' 
       ), 
       'notEmpty' => array(
        'rule' => 'notEmpty', 
        'message' => 'Please enter a password' 
       ) 
      ), 
      'confirm_password' => array(
       'passwordMatch' => array(
        'rule' => array('identicalFieldValues', 'password'), 
        'message' => 'The two passwords you entered do not match, please try again' 
       ), 
       'notEmpty' => array(
        'rule' => 'notEmpty', 
        'message' => 'Please confirm your password by entering it twice' 
       ) 
      ) 
    ); 
관련 문제