Raz*_*orm 5 validation cakephp
我有一个自定义验证规则来检查输入的两个密码是否相同,如果它们不是,我希望有一条消息"密码不匹配".
但是,规则有效,当密码不匹配时,只显示正常的错误消息,发生了什么?
var $validate=array(
'passwd2' => array('rule' => 'alphanumeric',
'rule' => 'confirmPassword',
'required' => true,
'allowEmpty'=>false));
function confirmPassword($data)
{
$valid = false;
if ( Security::hash(Configure::read('Security.salt') .$data['passwd2']) == $this->data['User']['passwd'])
{
$valid = true;
$this->invalidate('passwd2', 'Passwords do not match');
}
return $valid;
}
Run Code Online (Sandbox Code Playgroud)
它说"这个字段不能留空"
编辑:
奇怪的是,如果我将其中一个密码字段留空,则两条错误消息都会显示"此字段不能留空"
但是,如果我在两者中放入某些东西,那么它正确地说"密码不匹配"
我觉得你太复杂了.我是这样做的:
// In the model
public $validate = array(
'password' => array(
'minLength' => array(
'rule' => array('minLength', '8')
),
'notEmpty' => array(
'rule' => 'notEmpty',
'required' => true
)
),
'confirm_password' => array(
'minLength' => array(
'rule' => array('minLength', '8'),
'required' => true
),
'notEmpty' => array(
'rule' => 'notEmpty'
),
'comparePasswords' => array(
'rule' => 'comparePasswords' // Protected function below
),
)
);
protected function comparePasswords($field = null){
return (Security::hash($field['confirm_password'], null, true) === $this->data['User']['password']);
}
// In the view
echo $form->input('confirm_password', array(
'label' => __('Password', true),
'type' => 'password',
'error' => array(
'comparePasswords' => __('Typed passwords did not match.', true),
'minLength' => __('The password should be at least 8 characters long.', true),
'notEmpty' => __('The password must not be empty.', true)
)
));
echo $form->input('password', array(
'label' => __('Repeat Password', true)
));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5087 次 |
| 最近记录: |