0

我按照这篇文章进行密码确认,但 CakePHP 似乎跳过了我的 re_password 验证设置。

这是我的表格

<?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 字段没有任何发生

我删除了 equalToField 方法只是为了看看会发生什么。我什至没有收到错误,所以似乎 re_password 甚至没有被查看。

我不确定这是否与它有关,但是当我在密码字段中添加有关 re_password 的附加规则时,出现以下错误:“未定义索引: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

2 回答 2

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'
                )
           )
      );
于 2016-05-28T09:57:15.730 回答
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'];
}
于 2016-05-28T09:41:18.737 回答