Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel validate array values contained in list [duplicate]

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?

like image 451
reans Avatar asked Feb 27 '26 14:02

reans


1 Answers

Validate each string in the array:

'values.*' => 'string|in:name,title,description'
like image 96
reans Avatar answered Mar 01 '26 03:03

reans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!