v-6ren">
gpt4 book ai didi

php - Laravel Validator 规则中不同字段上的多个 "required_if"

转载 作者:搜寻专家 更新时间:2023-10-31 21:48:20 27 4
gpt4 key购买 nike

我正在为我的帖子字段创建一些规则,但我需要在一个规则中添加来自不同字段的 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 的副本)。

最佳答案

感谢 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);

关于php - Laravel Validator 规则中不同字段上的多个 "required_if",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50534598/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com