spatie / laravel-enum

Laravel support for spatie/enum
https://spatie.be/open-source
MIT License
344 stars 37 forks source link

Collection validation #61

Closed lucraraujo closed 3 years ago

lucraraujo commented 3 years ago

I have a list os value that I have encapsulated as enum. It's something like this:

/**
 * @method static self VALUE1()
 * @method static self VALUE2()
 * @method static self VALUE3()
 */
final class FooEnum extends Enum
{}

And I can select one or more os theses values, I'm trying to validade if in the values I receive in the controller are valid. The array I receive is something like this:

$many_foo=[
    0 => 'VALUE1',
    1 => 'VALUE23'
];

And my validation is something like this:

$request->validate([
   'many_foo' => ['required', new EnumRule(FooEnum::class)
]
Gummibeer commented 3 years ago

Hey,

you can use the asterisk to achieve this.

$request->validate([
   'many_foo' => ['required', 'array'],
   'many_foo.*' => [new EnumRule(FooEnum::class)],
]);
lucraraujo commented 3 years ago

@Gummibeer Thanks man! It worked! Sorry if a opened this issue on the wrong place, since my problem was a lack of knowledge on Laravel validation. Thanks a lot!