CakePHP跳过输入验证

时间:2013-08-16 18:55:17

标签: php cakephp

我跟着this post进行密码确认,但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-&gt; request-&gt; data)并设置了re_password字段。

2 个答案:

答案 0 :(得分: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'];
}

答案 1 :(得分: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'
                )
           )
      );