php Laravel 验证:存在附加列条件 - 自定义验证规则
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26121417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel validation: exists with additional column condition - custom validation rule
提问by Jonathon
Is there is a way of referencing another field when specifying the exists validation rule in Laravel? I want to be able to say that input a must exist in table a, input b must exist in table b AND the value for column x in table b must equal input a.
在 Laravel 中指定存在验证规则时,是否可以引用另一个字段?我希望能够说输入 a 必须存在于表 a 中,输入 b 必须存在于表 b 中并且表 b 中列 x 的值必须等于输入 a。
Best explained by example:
最好通过例子来解释:
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>'
);
So with my validation rules I want to be able to make sure that:
因此,根据我的验证规则,我希望能够确保:
game_id
exists within thegames
table (id
field)team1_id
exists within theteams
table (id
field) and thegame_id
column (in theteams
table) must equal the value of thegame_id
input.- As above for
team2_id
game_id
存在于games
表(id
字段)中team1_id
存在于teams
表(id
字段)中并且game_id
列(在teams
表中)必须等于game_id
输入的值。- 如上对于
team2_id
So, if in my form, I entered 1
for game_id
, I want to be able to ensure that the record within the teams table for both team1_id
and team2_id
have the value 1
for game_id
.
所以,如果在我的形式,我进入1
了game_id
,我希望能够确保两者的球队表中的记录team1_id
,并team2_id
具有价值1
的game_id
。
I hope this makes sense.
我希望这是有道理的。
Thanks
谢谢
采纳答案by Jarek Tkaczyk
You want a custom validation rule, and I would create a separate class for this. But for brevity here's pretty much the same using inline closure:
您需要自定义验证规则,我会为此创建一个单独的类。但为简洁起见,这里使用内联闭包几乎相同:
// 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;
});
Then use simply this:
然后简单地使用这个:
$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'
);
回答by roerjo
EDIT: Supposedly this does not work in Laravel 5.5. @user3151197 answer might do the trick.
编辑:据说这在 Laravel 5.5 中不起作用。@ user3151197 答案可能会奏效。
I'm using Laravel 5.4 and it has the ability to add a custom Ruleto the exists and unique rules. I think this came into existence some time in 5.3
我正在使用 Laravel 5.4,它能够将自定义规则添加到现有规则和唯一规则中。我认为这在 5.3 的某个时间出现了
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.
这是我的场景:我有一个电子邮件验证表,我想确保在同一行中存在传递的机器代码和激活代码。
Be sure to include use Illuminate\Validation\Rule;
务必包括 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'
];
The first arguement 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.
exists 方法中的第一个参数是表,第二个参数是我用于 'mc' 字段的自定义列名称。我通过了我想使用“use”关键字检查的第二列,然后在 aa where 子句中使用该字段。
This is pretty handy, because now I no longer need a custom Validation rule.
这非常方便,因为现在我不再需要自定义验证规则。
回答by Marcin Nabia?ek
As your rules are model property you need to make some change for them before running validator.
由于您的规则是模型属性,您需要在运行验证器之前对它们进行一些更改。
You could change your rules to:
您可以将规则更改为:
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}'
);
and now you will need to use loop to insert correct value instead of {$game_id}
string.
现在您需要使用循环来插入正确的值而不是{$game_id}
字符串。
I can show you how I did it in my case for editing rule:
我可以向您展示我在编辑规则的情况下是如何做到的:
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;
}
You can do the same in your case changing {$game_id}
into $data['game_id']
(in my case I changed {,id}
into ,$editId
你可以在你的情况下做同样的事情{$game_id}
变成$data['game_id']
(在我的情况下我变成{,id}
了,$editId
EDIT
编辑
Of course If you didn't have $rules
set as property you could simply do:
当然,如果您没有$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']
);
in place where you have your data set.
在您拥有数据集的地方。
回答by user3151197
try this its works for me
试试这个对我有用
'email'=>'required|unique:admintable,Email,'.$adminid.',admin_id',