$data =['identifier' = 'xxxxxxxxxx'];
,并且想要将encrypt($data['identifier'])
保存到表格info
的主键id
列中。在保存之前,我需要进行验证。规则
unique:info, id
不适用于此处,因此我想编写自定义验证规则。在自定义验证规则中,我先encrypt()
该值,然后使用unique
验证规则。我知道如何编写自定义验证规则,但是如何在我的自定义验证规则中使用
unique
验证规则呢?$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;
});
然后您可以像往常一样使用它:
'identifier' => "encrypted_unique:table,column,$this_id"
protected function validationData()
{
$all = parent::validationData();
$all['email'] = encrypt($all['email']);
return $all;
}
encrypt()
,就像我在问题中所述的那样。 - LF00Laravel有自定义验证规则(https://laravel.com/docs/8.x/validation#using-rule-objects) 例如,我有一个名为clients的表格,其中有两个唯一字段使用Laravel的加密服务进行加密(https://laravel.com/docs/8.x/encryption),因为它被加密了,所以无法应用验证方法(https://laravel.com/docs/8.x/validation#rule-unique)的唯一指令。这些字段是code_client和email
这就是实现自定义验证规则的原因。 此服务有两种方法:passes和message。方法passes接受两个变量:$attributes(要验证的字段)和$value(字段的值),并返回true或false。方法message在失败的情况下检索消息。
在我提到的clients示例中,请按照以下步骤操作:
php artisan make:rule ValidateFieldsClients
在composer创建的ValidateFieldsClients类中,我必须声明一个方法来验证passes中的字段,我使用此方法来验证两个字段(code_client和email)。
接下来,我完成了用于在视图中向用户检索问题的消息方法。
此外,我声明了一个属性$field来标识哪个字段存在错误。
ValidateFieldsClients类示例:
/***/class ValidateFieldsClients implements Rule{protected $field; /**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$clients = client::all();
$this->field = $attribute;
foreach ($clients as $client ) {
if ($value == Crypt::decryptString($client->$attribute)) return false;
}
return true;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return strtoupper($this->field).' exists, check.';
}
}
然后,我使用表单请求验证来进行验证(https://laravel.com/docs/8.x/validation#form-request-validation)
php artisan make:request ClientRequest
在最近创建的类的验证方法中:
class ClientRequest extends FormRequest
{ /**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true; }
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'code_client'=> ['required', new ValidateFieldsClients],
'email'=>['required', new ValidateFieldsClients],
];
}
最后,在控制器中:
public function store(ClientRequest $request)
{ $clientRequest = $request->validated();
foreach ($clientRequest as $key => $client) {
$encryptedClient[$key] = Crypt::encryptString($client);
}; client::create($encryptedClient+ [
'idorga' => 1,
'idcrea' => 1,
'idmodifica' => 1
]);
return redirect('clientes/create')->with('success', 'Registro creado correctamente');
//return redirect('cuadros')->with('success', 'Registro exitoso!');
}
id
的初始值是什么?它是int
类型,那么它会被加密吗?! - SaidbakRunique
规则只是一个验证规则,你可以根据自己的具体需求创建一个与之类似的规则。 - ceejayoz