Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Allow passing Enum casts to Rule::enum()->only() and ->except() #52073

Merged
merged 3 commits into from
Jul 10, 2024

Conversation

bakerkretzmar
Copy link
Contributor

This PR adds support for passing model properties that use Laravel's built-in AsEnumArrayObject and AsEnumCollection casts directly into Rule::enum()->only() and Rule::enum()->except():

validator([
    'user_type' => $request->input('user_type')
], [
    Rule::enum(UserType::class)->only(
        Tenant::first()->supported_user_types // <-- new in this PR, no need to call ->toArray() here
    ),
]);

In that example UserType is an enum and the Tenant model has this cast:

protected $casts = [
    'supported_user_types' => AsEnumArrayObject::class.':'.UserType::class,
];

Right now you can only pass enums or plain arrays to ->only() and ->except(), which means if you have a cast model property you have to always call ->toArray() on it, which is tedious.

The AsEnumArrayObject and AsEnumCollection casts and Rule::enum()->only() feel like they were made to be used together, so this PR makes that experience a bit smoother.

@taylorotwell taylorotwell merged commit 1dd5027 into laravel:11.x Jul 10, 2024
30 checks passed
@bakerkretzmar bakerkretzmar deleted the enum-only-except-arrayable branch July 10, 2024 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants