Laravel validate at least one item in a form array

后端 未结 8 1024
借酒劲吻你
借酒劲吻你 2020-12-30 11:12

I have a form with a series of numbers in an array:




        
8条回答
  •  無奈伤痛
    2020-12-30 11:58

    The accepted answer is ok, and it is working fine, but if you don't want to create function and other stuff then the another workaround which i have used is

    that you can directly state the element of array as items.0 and make that input required, it will not care about other inputs but it will make first input required, it is not usable in all the cases but in my case it is useful.

    in your OrderCreateRequest:

    public function rules()
    {
       return [
           "items.0" => "required",
       ];
    }
    

    And if your items are not dynamic and not in very large amount then you can use required_without_all as below:

    public function rules()
    {
       return [
           "items.0" => "required_without_all:items.1,items.2",
           "items.1" => "required_without_all:items.0,items.2",
           "items.2" => "required_without_all:items.0,items.1",
       ];
    }
    

提交回复
热议问题