Home > Back-end >  Laravel validate array values contained in list
Laravel validate array values contained in list

Time:10-06

I am passing an array in a request to my api. Each value within the array must be within a pre-defined list.

If my list is: name,description,title

name, title //valid
different, title //invalid

I tried array|in:name,description,title but I think for that I can only pass a string.

Can I do this without using a custom rule?

CodePudding user response:

Validate each string in the array:

'values.*' => 'string|in:name,title,description'

CodePudding user response:

Have a look at "Validating Nested Array Input"

If I understand you correctly your validation rules should be (untested)

[
   '*.name' => 'required|string',
   '*.description' => 'required|string',
]

Maybe you also want to exclude unvalidated Keys

  • Related