【问题标题】:CakePHP skipping validation for an inputCakePHP 跳过输入验证
【发布时间】:2013-08-16 18:55:16
【问题描述】:

我关注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->request->data) 并设置了 re_password 字段。

【问题讨论】:

  • 您没有发布您的控制器代码。应该是问题的根源。 PS:您应该始终提及您正在使用的确切 cakephp 版本。
  • @mark 感谢您在控制器中查看的建议。我忘记了我之前在那里对密码进行了哈希处理然后保存了。我删除了它,现在验证工作正常。
  • +35 虚名给@mark!

标签: php cakephp


【解决方案1】:

对于 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'];
}

【讨论】:

    【解决方案2】:

    这对我有用,试试这个..

    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'
                    )
               )
          );
    

    【讨论】:

      猜你喜欢
      • 2020-12-03
      • 2013-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-13
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多