我按照这篇文章进行密码确认,但 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 字段。