Laravel验证:存在附加列条件 - 自定义验证规则

Jon*_*hon 14 php validation laravel laravel-4 laravel-validation

在Laravel中指定存在的验证规则时,是否有一种引用另一个字段的方法?我希望能够说输入a必须存在于表a中,输入b必须存在于表b中,并且表b中列x的值必须等于输入a.

最好用例子解释:

public $rules = array(
    'game_id' => 'required|exists:games,id',
    'team1_id' => 'required|exists:teams,id,game_id,<game_id input value here>',
    'team2_id' => 'required|exists:teams,id,game_id,<game_id input value here>'
);
Run Code Online (Sandbox Code Playgroud)

因此,根据我的验证规则,我希望能够确保:

  • game_id存在于games表(id字段)中
  • team1_id存在于teams表(id字段)中,game_id列(在teams表中)必须等于game_id输入的值.
  • 如上所述 team2_id

所以,如果在我的形式,我进入1game_id,我希望能够确保两者的球队表中的记录team1_id,并team2_id具有价值1game_id.

我希望这是有道理的.

谢谢

roe*_*rjo 15

编辑:据说这在Laravel 5.5中不起作用.@ user3151197回答可能会解决问题.

我正在使用Laravel 5.4,它能够将自定义规则添加到存在且唯一的规则中.我认为这在5.3中出现了一段时间

这是我的方案:我有一个电子邮件验证表,我想确保在同一行上存在传递的机器代码和激活码.

一定要包括 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'                          
];
Run Code Online (Sandbox Code Playgroud)

exists方法中的第一个争论是表,第二个是我用于'mc'字段的自定义列名.我使用'use'关键字传递我要检查的第二列,然后在aa子句中使用该字段.

这非常方便,因为现在我不再需要自定义验证规则.

  • 这不适用于Laravel 5.5 (2认同)

Jar*_*zyk 12

您需要一个自定义验证规则,我会为此创建一个单独的类.但为简洁起见,使用内联闭包几乎相同:

// give it meaningful name, I'll go with game_fixture as an example
Validator::extend('game_fixture', function ($attribute, $value, $parameters, $validator) 
{
    if (count($parameters) < 4)
    {
        throw new \InvalidArgumentException("Validation rule game_fixture requires 4 parameters.");
    }

    $input    = $validator->getData();
    $verifier = $validator->getPresenceVerifier();

    $collection = $parameters[0];
    $column     = $parameters[1];
    $extra      = [$parameters[2] => array_get($input, $parameters[3])];

    $count = $verifier->getMultiCount($collection, $column, (array) $value, $extra);

    return $count >= 1;
});
Run Code Online (Sandbox Code Playgroud)

然后使用这个:

$rules = array(
    'game_id' => 'required|exists:games,id',

    // last parameter here refers to the 'game_id' value passed to the validator
    'team1_id' => 'required|game_fixture:teams,id,game_id,game_id',
    'team2_id' => 'required|game_fixture:teams,id,game_id,game_id'
);
Run Code Online (Sandbox Code Playgroud)


Mar*_*łek 10

由于您的规则是模型属性,您需要在运行验证器之前对它们进行一些更改。

您可以将规则更改为:

public $rules = array(
    'game_id' => 'required|exists:games,id',
    'team1_id' => 'required|exists:teams,id,game_id,{$game_id}',
    'team2_id' => 'required|exists:teams,id,game_id,{$game_id}'
);
Run Code Online (Sandbox Code Playgroud)

现在您需要使用循环来插入正确的值而不是{$game_id}字符串。

我可以向您展示我在编辑规则的情况下是如何做到的:

public function validate($data, $translation, $editId = null)
{
    $rules = $this->rules;

    $rules = array_intersect_key($rules, $data);

    foreach ($rules as $k => $v) {
        $rules[$k] = str_replace('{,id}',is_null($editId) ? '' : ','.$editId , $v);
    }

    $v = Validator::make($data, $rules, $translation);

    if ($v->fails())
    {
        $this->errors = $v->errors();
        return false;
    }

    return true;
}
Run Code Online (Sandbox Code Playgroud)

你可以在你的情况下做同样的事情{$game_id}变成$data['game_id'](在我的情况下我变成{,id},$editId

编辑

当然,如果您没有$rules设置为属性,您可以简单地执行以下操作:

$rules = array(
    'game_id' => 'required|exists:games,id',
    'team1_id' => 'required|exists:teams,id,game_id,'.$data['game_id'],
    'team2_id' => 'required|exists:teams,id,game_id,'.$data['game_id']
);
Run Code Online (Sandbox Code Playgroud)

在您拥有数据集的地方。


小智 8

检查NULL 条件

'game_id' => 'required|exists:games,id,another_column,NULL',
Run Code Online (Sandbox Code Playgroud)

您可以使用 (,) 列名称和值添加更多条件。


小智 5

另一种方法你可以尝试这些

public $rules = array(
   'game_id' => 'required|exists:games,id',
   'team1_id' => 'required|exists:teams,id,game_id,'.$request->game_id,
   'team2_id' => 'required|exists:teams,id,game_id,'.$request->game_id
);
Run Code Online (Sandbox Code Playgroud)