在Laravel中指定存在的验证规则时,是否可以引用另一个字段?
Is there a way of referencing another field when specifying the exists validation rule in Laravel?
我的请求:
public function rules() { return [ 'numero_de_somme' => 'unique:personnels,numero_de_somme|exists:fonctionnaire,num_somme', 'cin' => 'unique:personnels,cin|exists:fonctionnaire,cin', ]; }在我的验证规则中,我希望能够确保:
in my validation rules I want to be able to make sure that:
- num_somme 存在于 fonctionnaire 表中 功能表中存在
- cin ,并且 cin 输入必须为在 num_somme 的同一行上
- num_somme exists within the fonctionnaire table
- cin exists within the fonctionnaire table and cin input must be on the same row of the num_somme
num_somme:12345cin:s89745
num_somme : 12345 cin : s89745
num_somme:78945电影:U10125
num_somme : 78945 cin : U10125
解释:例如
- 第一种情况,如果输入 num_somme = 12345并且 cin = U10125,则验证必须失败
- 第二种情况,如果输入 num_somme = 12345并且 cin = s89745,则验证必须成功
- 1st scenario if the input num_somme = 12345 and cin = U10125 the validation must fail
- 2nd scenario if the input num_somme = 12345 and cin = s89745 the validation must success
我希望这是有道理的.
谢谢
推荐答案我今天遇到了同样的需求,我认为我有使用验证规则类的解决方案:规则示例.
I came across the same need today and I think I have a solution using the validation Rule class: Rule example.
这是我的情况:我有一个电子邮件验证表,我想确保传递的机器代码和激活代码在同一行中.
Here is my scenario: I have an email verifications table and I want to ensure that a passed machine code and activation code exist on the same row.
请确保包含 use Illuminate \ Validation \ Rule;
$activationCode = $request->activation_code; $rules = [ 'mc' => [ 'required', Rule::exists('email_verifications', 'machineCode') ->where(function ($query) use ($activationCode) { $query->where('activationCode', $activationCode); }), ], 'activation_code' => 'required|integer|min:5', 'operating_system' => 'required|alpha_num|max:45' ];exists方法中的第一个参数是表格,第二个参数是我用于"mc"字段的自定义列名称.我使用"use"关键字传递要检查的第二列,然后在where子句中使用该字段.
The first argument in the exists method is the table and the second is the custom column name I'm using for the 'mc' field. I pass the second column I want to check using the 'use' keyword and then use that field in a a where clause.
更多推荐
Laravel验证:存在两列相同的行
发布评论