Validate array of inputs in form in Laravel 5.7

前端 未结 6 1400
生来不讨喜
生来不讨喜 2021-01-20 01:59

My form has the same input field multiple times. My form field is as follows:




        
6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-20 03:05

    In fact, it's enough to use:

    $validator = Validator::make($request->all(),[
            'items' => 'required|array'
        ]);
    

    The changes made:

    • use items instead of items.* - you want to set rule of general items, if you use items.* it means you apply rule to each sent element of array separately
    • removed size:1 because it would mean you want to have exactly one element sent (and you want at least one). You don't need it at all because you have required rule. You can read documentation for required rule and you can read in there that empty array would case that required rule will fail, so this required rule for array makes that array should have at least 1 element, so you don't need min:1 or size:1 at all

提交回复
热议问题