I'm using the new enum
class in my Laravel application and want to validate optional parameters. I'm able to validate enum parameters if I pass them, but if I don't pass anything, I get the error, "Undefined array key".
My enum class is:
enum SortBy: string {
case DateEntered = 'date_entered';
}
And my validation is:
$validatedData = $request->validate([
'sortBy' => [new Enum(SortBy::class)]
];
This works if I make a request with my "sortBy" parameter equal to "dated_entered". If I omit "sortBy" though, I get the "undefined" error.
I tried this as well, but no luck:
$validatedData = $request->validate([
'sortBy' => [new Enum(SortBy::class), 'nullable']
];
Other things I've tried:
$validatedData = $request->validate([
'sortBy' => ['exclude_if:sortBy,null', new Enum(SortBy::class)]
];
$validatedData = $request->validate([
'sortBy' => ['nullable', new Enum(SortBy::class)]
];
$validatedData = $request->validate([
'sortBy' => ['sometimes', new Enum(SortBy::class)]
];