我输入了$data =['identifier' = 'xxxxxxxxxx'];,并希望将encrypt($data['identifier'])保存到表info主id列中。
在保存之前我要验证一下。规则unique:info, id在这里不合适,所以我想编写一个自定义验证规则。在自定义验证规则中,我首先encrypt()值,然后使用unique验证规则。
我知道如何编写自定义验证规则,但是如何在我的自定义验证规则中使用unique验证规则?
发布于 2017-07-04 13:24:22
规则“唯一”和“存在”使用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"发布于 2017-06-28 07:56:45
假设您有一个验证输入的ModuleRequest,则可以在这个类中编写此方法。
protected function validationData()
{
$all = parent::validationData();
$all['email'] = encrypt($all['email']);
return $all;
}发布于 2021-12-12 14:43:54
Laravel有自定义验证规则(https://laravel.com/docs/8.x/validation#using-rule-objects),例如,我有一个名为clients的表,该表使用Laravel的加密服务(https://laravel.com/docs/8.x/encryption)拥有两个唯一字段ecnrypt,而且由于它是加密的,所以无法使用唯一的验证方法指令(https://laravel.com/docs/8.x/validation#rule-unique)。这些字段是code_client和email
这就是实现自定义验证规则的原因。此服务有两种方法:传递和消息。该方法通过两个变量:$attributes (将de用于验证)和$value (获取字段的de值),并返回true或false。方法在发生故障时检索消息。
在我提到的客户机示例中,遵循下面的步骤:
https://stackoverflow.com/questions/44793365
复制相似问题