phplaravellaravel-validation

Laravel validate array values contained in list


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 as opposed to an array.

I understand I can use:

'values' => 'in:name',

However this is when I am passing a string in my json request body eg. { "values": "name"}. I am trying to pas an array eg. { "field": ["name", "description"]}

Can I do this without using a custom rule?


Solution

  • Validate each string in the array:

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