gpt4 book ai didi

cakephp 自定义验证规则消息

转载 作者:行者123 更新时间:2023-12-04 17:40:55 24 4
gpt4 key购买 nike

我有一个自定义验证规则来检查输入的两个密码是否相同,如果它们不相同,我希望有一条消息显示“密码不匹配”。

该规则有效,但是,当密码不匹配时,它只会显示正常的错误消息,这是怎么回事?

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;
}

它说“此字段不能留空”

编辑:

奇怪的是,如果我将其中一个密码字段留空,两条错误消息都会显示“此字段不能留空”

但是,如果我在两者中都放了一些东西,那么它会正确地显示“密码不匹配”

最佳答案

我认为你把它弄得太复杂了。这是我如何做到的:

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

关于cakephp 自定义验证规则消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3995512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com