Password Confirm Validation CakePHP

陌路散爱 提交于 2019-12-07 12:23:07

问题


I have searched far and wide, tried every trick in the book, but I still can't get my CakePHP application to perform simple Password Confirm validation. I've tried creating a custom validation rule like this:

'passwordequal' => array('rule' => 'checkpasswords' , 'message' => 'Passwords Do Not Match')

Then defined 'checkpasswords' like this:

public function checkpasswords(){

    if(strcmp($this->data['User']['new_password'],$this->data['User']['confirm_password']) == 0 )
    {
        return true;
    }
    return false;
}

'new_password' and 'confirm_password' are the password input fields. This didn't work. Then I tried one in which I hashed the 'confirm_password'. That didn't work either. I have other 'rules' as well that are not being validated, like 'notempty', which I believe is one of the standard CakePHP rules. Can anybody please help. I know this question has been asked a few times but none of those solutions have worked for me. CakePHP documentation hasn't helped much either.


回答1:


two fields in view file

echo $this->Form->input('password');
echo $this->Form->input('repass');

Model file

<?php
class Post extends AppModel {
    public $validate = array(
        'repass' => array(
            'equaltofield' => array(
            'rule' => array('equaltofield','password'),
            'message' => 'Require the same value to password.',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            'on' => 'create', // Limit validation to 'create' or 'update' operations
            )
        )
    );

function equaltofield($check,$otherfield)
    {
        //get name of field
        $fname = '';
        foreach ($check as $key => $value){
            $fname = $key;
            break;
        }
        return $this->data[$this->name][$otherfield] === $this->data[$this->name][$fname];
    } 
}?>



回答2:


Seems like your model is not loading correctly and using a dynamically generated model.

Comparing passwords in 2.x is nothing more than comparing any two fields as cake no longer hashes the pw automatically.

Can you confirm your validation method is being run, seems like its not especially if simple things like notEmpty is not working.



来源:https://stackoverflow.com/questions/17185246/password-confirm-validation-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!