我正在为我的帖子字段创建一些规则,但我需要在一个规则中的不同字段中添加2个“required_if”,例如它是“AND”或“&& ;”但来自不同的领域。
$validator = $this->validate([
'form_country' => 'required|array|min:1',
'form_country.*' => 'required|numeric',
'form_tipo' => 'required|array|min:1',
'form_tipo.*' => 'required|numeric|max:10', ,
'form_fecha_iti_back' => 'required|array|min:1',
//so if the actual form_country is 1 AND the actual form_tipo is 3 or 5
'form_fecha_iti_back.*' => 'required_if:form_country.*,1|required_if:form_tipo.*,3,5',
]);
(它不是this question的副本)。
答案 0 :(得分:0)
感谢laracasts'用户https://laracasts.com/@Cronix我设法找到了解决方案,并希望它可以帮助许多其他用户,因为它帮助了我。
//so first I create all the common rules within $rules
$rules = [
'form_gender' => 'required|array|min:1',
'form_gender.*' => 'required|string|min:4|max:6',
// others
];
//then get the requests from the needed fields
$fecha_back = request('form_fecha_iti_back');
$formCountry = request("form_country");
$form_tipo_apoyo = request("form_tipo_apoyo");
//now let's valid if we have any value and produce the custom rules according to the conditions.
if($fecha_back){
foreach ($fecha_back as $id => $fecha) {
if($formCountry[$id] == 1 && $form_tipo_apoyo[$id] == 3 || $form_tipo_apoyo[$id] == 5){
$rules["form_fecha_iti_back.".$id.""] = "required|date|after:form_fecha_iti_depart.*".$id."";
} else{
$rules["form_fecha_iti_back.".$id.""] = "nullable|date|after:form_fecha_iti_depart.".$id."";
}
}
}
//finally we got to validate all the rules
$validator = $this->validate($rules);