Skip to content

[9.x] Allow instance of Enum pass Enum Rule #42906

Merged
taylorotwell merged 2 commits into
laravel:9.xfrom
cbaconnier:validate-enum-instance
Jun 22, 2022
Merged

[9.x] Allow instance of Enum pass Enum Rule #42906
taylorotwell merged 2 commits into
laravel:9.xfrom
cbaconnier:validate-enum-instance

Conversation

@cbaconnier

Copy link
Copy Markdown
Contributor

Given this scenario, the validation for country currently fail as it expect the value of CountryEnum.
Laravel will automatically cast the enum instance when saving the model, therefore, it should be safe to validate by it's instance.

$action = new CreateNewUser();

$action->create([
    'country' => CountryEnum::CH,
    'email'   => 'john.doe@example.org',
]);
public function create(array $input)
{
    Validator::make($input, [
        'country' => ['required',  Enum(CountryEnum::class)],
        'email'   => [...],
    ])->validate();
}

@taylorotwell
taylorotwell merged commit f87031b into laravel:9.x Jun 22, 2022
@cbaconnier
cbaconnier deleted the validate-enum-instance branch June 22, 2022 18:51
turanjanin pushed a commit to turanjanin/laravel-framework that referenced this pull request Jun 23, 2022
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.

2 participants