代码之家  ›  专栏  ›  技术社区  ›  Mike Kevin Castro

Laravel数组元素验证

  •  0
  • Mike Kevin Castro  · 技术社区  · 6 年前

    我想验证一下 audition_dates 前一个字段 shoot_dates

    我知道我可以通过这些规则来验证阵列:

    public function store(Request $request)
    {
        $validatedData = $request->validate([
          'shoot_dates' => 'array',
          'shoot_dates.*' => 'date',
          'audition_dates' => 'array',
          'audition_dates.*' => 'date'
        ]);
    
        // The request is valid...
    }
    

    $rules = [
     'start_date' => 'after:tomorrow',
     'end_date' => 'after:start_date'
    ];
    

    但我不知道如何在数组上实现它们。

    0 回复  |  直到 6 年前
        1
  •  0
  •   LuisE    6 年前

    对于您的代码,以及 Laravel Documentation :

    Validator::make($request->all(), $rules)->validate();
    

    我们把它放在函数的开头。您可以在我提供的文档链接中阅读有关如何创建规则的更多信息。

    'audition_dates' =>'required|date|before:date'
    

    日期必须符合 strtotime

    'audition_dates' => 'required|date|before:shoot_dates'
    
        2
  •  0
  •   Mike Kevin Castro    6 年前

    我找到了解决办法。

    [
      'audition_dates'   => 'required|array',
      'audition_dates.*' => 'date|after:tomorrow',
      'shoot_dates'      => 'required|array',
      'shoot_dates.*'    => 'date|after:audition_dates.*',
    ];