I want to check if all the values of my input, an array, are in another array. For example:
$my_arr = ['item1', 'item2', 'item3', 'item4'];
Validator::make($request, [
'item' => [ /* what do i put here??? */ ],
]);
I don't think the rule in
works, because that expects a single value as input, not an array. in_array
doesn't work either. I've tried creating a custom rule or closure, but neither of those allow me to pass in a parameter (the array I want to check against). Especially since I want this same functionality for multiple arrays, for different inputs, it would be nice to have a generic rule that works for any array I give it.
If that's not possible, I suppose I need to create a rule for each specific array and use !array_diff($search_this, $all)
as this answer says. Is there an alternative to this?
question from:
https://stackoverflow.com/questions/65930937/check-if-values-in-array-are-in-another-array-using-laravel-validation 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…