8000 Allow enum type for validation groups · Issue #45494 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Allow enum type for validation groups #45494

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

Closed
Guuzen opened this issue Feb 21, 2022 · 7 comments
Closed

Allow enum type for validation groups #45494

Guuzen opened this issue Feb 21, 2022 · 7 comments

Comments

@Guuzen
Copy link
Guuzen commented Feb 21, 2022

Description

Validation groups is often predefined set of strings. Seems like it make sense to use enum for this. Probably backed string enums.
Right now there is an error if you try to use enums for groups.

TypeError : Illegal offset type
vendor/symfony/validator/Mapping/GenericMetadata.php:167

Example

enum MyEnum: string
{
    case CASE_ONE = 'CASE_ONE';
    case CASE_TWO = 'CASE_TWO';
}

#[Assert\Callback(groups: [MyEnum::CASE_ONE])]
class Foo
{    
}
@nicolas-grekas
Copy link
Member

Closing as explained in the linked PR: this is an engine-level consideration that should be fixed either by https://wiki.php.net/rfc/fetch_property_in_const_expressions or https://wiki.php.net/rfc/auto-implement_stringable_for_string_backed_enums

@nicolas-grekas nicolas-grekas closed this as not planned Won't fix, can't repro, duplicate, stale Jun 22, 2022
@ThomasLandauer
Copy link
Contributor

@nicolas-grekas I think this should be reconsidered. Is there a reason for the Enum to be Backed? What about making Pure Enums possible as validation group?:

enum Group
{
    case Foo;
    case Bar;
}

#[Assert\Callback(groups: [Group::Foo])]
class Whatever
{    
}

@derrabus
Copy link
Member
derrabus commented Mar 9, 2023

A group is a string and we cannot guess the string representation of a non-backed enum.

@ThomasLandauer
Copy link
Contributor

Yes, but the question is if it would be possible to also allow the enum type (besides string) as group.

@derrabus
Copy link
Member
derrabus commented Mar 9, 2023

Why? What's the use case?

@ThomasLandauer
Copy link
Contributor

Getting rid of strings - this looks like the "natural" use case for all enums to me :-)
If you have many validation groups, you can easily mistype one. With enums, static analysis would report errors immediately.

@derrabus
Copy link
Member
derrabus commented Mar 9, 2023

What happened to constants for that matter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
0