首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Laravel Validator规则中不同字段上的多个"required_if“

Laravel Validator规则中不同字段上的多个"required_if“
EN

Stack Overflow用户
提问于 2018-05-26 01:57:26
回答 1查看 2K关注 0票数 4

我正在为我的帖子字段创建一些规则,但我需要在一个规则中添加来自不同字段的2个"required_if“,比如如果它是"AND”或"&&“,但来自不同的字段。

代码语言:javascript
运行
复制
$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的副本)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-30 00:53:05

感谢laracasts的用户https://laracasts.com/@Cronix,我设法找到了解决方案,并希望它能像帮助我一样帮助许多其他用户。

代码语言:javascript
运行
复制
//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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50534598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档