我已输入$data =['identifier' = 'xxxxxxxxxx'];
,并希望将encrypt($data['identifier'])
保存到表info
主id
列。
我在保存之前需要验证。规则unique:info, id
不适用于此,因此我想编写自定义验证规则。在自定义验证规则中,我首先encrypt()
值,然后使用unique
验证规则。
我知道如何编写自定义验证规则,但如何在自定义验证规则中使用unique
验证规则?
答案 0 :(得分:2)
规则"独特"和"存在"使用DatabasePresenceVerifier类。因此,您不需要真正扩展唯一规则,只需访问此状态验证程序即可。例如:
Validator::extend('encrypted_unique', function ($attribute, $value, $parameters, $validator) {
list ($table, $column, $ignore_id) = $parameters; // or hard-coded if fixed
$count = $validator->getPresenceVerifier()->getCount($table, $column, encrypt($value), $ignore_id);
return $count === 0;
});
然后你可以像往常一样使用它:
'identifier' => "encrypted_unique:table,column,$this_id"
答案 1 :(得分:1)
假设您有一个 ModuleRequest 来验证您的输入,您可以在此类中编写此方法
protected function validationData()
{
$all = parent::validationData();
$all['email'] = encrypt($all['email']);
return $all;
}