How do I validate Array inputs in Laravel 5.0

老子叫甜甜 提交于 2019-12-09 02:19:28

As I know, this[.*] notation was introduced in Laravel 5.2+. So, to achieve the array validation, you would need to add custom rules. Reference Link

public function rules()
{
  $rules = [
    'name' => 'required|max:255',//add more static rules as you need
  ];

  foreach($this->request->get('TB1_a') as $key => $val)
  {
    $rules['TB1_a.'.$key] = 'required_with:TB1_b.'.$key;
  }

  foreach($this->request->get('TB1_b') as $key => $val)
  {
    $rules['TB1_b.'.$key] = 'required_with:TB1_a.'.$key;
  }

  /*
  To combine the both validation rules in 1 loop. considering number of both fields are always equal
      foreach($this->request->get('TB1_a') as $key => $val)
      {
        $rules['TB1_a.'.$key] = 'required_with:TB1_b.'.$key;
        $rules['TB1_b.'.$key] = 'required_with:TB1_a.'.$key;
      }  
  */
  return $rules;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!