Laravel validate at least one item in a form array

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

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




        
8条回答
  •  孤独总比滥情好
    2020-12-30 11:49

    You can set your rules for array and foreach elements.

    public function rulez(Request $req) {
            $v = Validator::make($req->all(), [
                'items'   => 'required|array|min:1',
                'items.*' => 'required|integer|between:0,10'
    
            ]);
    
            if ($v->fails())
                return $v->errors()->all();
    }
    

    First rule say that items must be an array with 1 element at least. Second rule say that each elements of items must be an integer between 0 and 10.

    If the rules seem not to work, try to dump your $req->all().

    dump($req->all());
    

提交回复
热议问题