Built-in input validation rule for enum values #39897
-
When validating user input you may want to ensure that a value in the request comes from a list of supported values. This list has been made easier to define with PHP 8.1 in the form of native enums, more specifically backed enums: enum Suit: string
{
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
} If I want to validate that a property in the request body matches one of these values, I can currently do that with the Rule:in(array_column(Suit::cases(), 'value')) This gets all the cases for the enum, then collects their value properties in an array, which is compatible with Rule:enumValue(Suit::class) This would allow the property to be either |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
If this feature request is accepted I can send a PR with the changes needed. |
Beta Was this translation helpful? Give feedback.
-
https://github.com/laravel/framework/releases/tag/v8.69.0 added an Enum validation rule |
Beta Was this translation helpful? Give feedback.
-
Enum Validation Rules only works with the BackedEnum Interface |
Beta Was this translation helpful? Give feedback.
https://github.com/laravel/framework/releases/tag/v8.69.0 added an Enum validation rule