Laravel验证:与附加列条件存在 - 自定义验证规则
在Laravel中指定存在验证规则时是否有引用另一个字段的方法?我想能够说输入a必须存在于表a中,输入b必须存在于表b中,并且表b中列x的值必须等于输入a。Laravel验证:与附加列条件存在 - 自定义验证规则
最好的例子来说明:
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>'
);
与我的验证规则
所以我希望能够确保:
-
game_id
的games
表(id
场)中存在 -
team1_id
存在于teams
表(id
字段)和game_id
列(在teams
表)必须等于game_id
输入的值。 - 如上所述,对于
team2_id
所以,如果在我的形式,我进入1
为game_id
,我希望能够确保两者team1_id
和team2_id
球队表中的记录具有价值1
game_id
。
我希望这是有道理的。
感谢
你想要一个custom validation rule,我会为此创建一个单独的类。但为了简便起见这里是相当多的使用内联封相同:
// 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;
});
然后使用简单:
$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'
);
这似乎工作得很好,谢谢。我将不得不阅读自定义验证规则。应该在哪里放置自定义验证类?欢呼声 – Jonathon 2014-09-30 14:22:27
你可能有一些结构(或应该有)为你的自定义代码,所以把它放在那里。在Laravel 5中,您将创建一个新的服务提供商,但这是不同的故事。 – 2014-09-30 14:24:55
当你的规则是,你需要在运行验证之前做出一些改变他们模型属性。
你可以改变你的规则:
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}'
);
,现在你需要使用循环插入,而不是{$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;
}
你可以做你的情况改变{$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']
);
在你有你的数据集的地方。
编辑:据说,这不Laravel 5.5工作。 @ user3151197答案可能会诀窍。
我使用的是Laravel 5.4,它能够将自定义Rule添加到存在和唯一规则中。我认为这在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'
];
第一arguement中存在的方法是表,第二个是我使用了“MC”字段自定义列名。我通过使用'use'关键字检查第二列,然后在where子句中使用该字段。
这非常方便,因为现在我不再需要自定义验证规则。
这不适用于Laravel 5.5 – 2017-11-04 23:53:03
@AaronHill,您是如何在laravel v5.5中做的? – LINKeRxUA 2018-01-09 13:33:29
试试这个它的事业,对我来说
'email'=>'required|unique:admintable,Email,'.$adminid.',admin_id',
我一直在使用这种格式,因为5.3,它为我工作。我很确定它是有效的,尽管我没有在他们的文档中看到它。 $ this-> mymodel-> create_rules ['company_id'] ='required | exists:companies,id,type_id,'。 $ ID; 这意味着company_id必须存在于其自己的表中,并且type_id字段必须是$ id的值。 – 2017-07-11 13:27:27