Kri*_*ofe 5 php validation input laravel
我有输入$data =['identifier' = 'xxxxxxxxxx'];,并希望保存encrypt($data['identifier'])到表info主id列.
我要在保存之前验证.规则unique:info, id不适用于此,因此我想编写自定义验证规则.在自定义验证规则中,我encrypt()首先使用值,然后使用unique验证规则.
我知道如何编写自定义验证规则,但如何unique在自定义验证规则中使用验证规则?
规则“unique”和“exists”使用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;
});
Run Code Online (Sandbox Code Playgroud)
然后你就可以像往常一样使用它:
'identifier' => "encrypted_unique:table,column,$this_id"
Run Code Online (Sandbox Code Playgroud)