Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Form] Add the EnumType #43095

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

Merged
merged 1 commit into from
Sep 21, 2021
Merged

[Form] Add the EnumType #43095

merged 1 commit into from
Sep 21, 2021

Conversation

derrabus
Copy link
Member

@derrabus derrabus commented Sep 19, 2021

Q A
Branch? 5.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets N/A
License MIT
Doc PR TODO

This PR adds a new EnumType that enables the form component to configure a ChoiceType for a native PHP enum.

Example:

enum Suit: string
{
    case Hearts = 'H';
    case Diamonds = 'D';
    case Clubs = 'C';
    case Spades = 'S';
}

enum Rank: string
{
    case Ace = 'A';
    case King = 'K';
    case Queen = 'Q';
    case Jack = 'J';
    case Ten = 'X';
    case Nine = '9';
    case Eight = '8';
    case Seven = '7';
    case Six = '6';
    case Five = '5';
    case Four = '4';
    case Three = '3';
    case Two = '2';
}

final class Card
{
    public ?Suit $suit = null;
    public ?Rank $rank = null;
}
$form = $this
    ->createFormBuilder(null, ['data_class' => Card::class])
    ->add('suit', EnumType::class, ['class' => Suit::class, 'required' => false, 'expanded' => true])
    ->add('rank', EnumType::class, ['class' => Rank::class, 'required' => false])
    ->add('submit', SubmitType::class)
    ->getForm()
    ->handleRequest($request)
;

Bildschirmfoto 2021-09-19 um 18 06 39

@derrabus derrabus force-pushed the feature/form-enum-type branch 2 times, most recently from e14f102 to 51ef328 Compare September 19, 2021 16:27
@stof
Copy link
Member

stof commented Sep 19, 2021

please also add a test covering the case of enums backed by integers rather than strings.

@derrabus derrabus force-pushed the feature/form-enum-type branch 2 times, most recently from 268a8f4 to 55b0deb Compare September 19, 2021 16:51
@derrabus
Copy link
Member Author

please also add a test covering the case of enums backed by integers rather than strings.

Good idea, done!

@derrabus derrabus force-pushed the feature/form-enum-type branch 3 times, most recently from 7fd97b3 to 0f08e0a Compare September 19, 2021 17:12
@derrabus derrabus force-pushed the feature/form-enum-type branch from 0f08e0a to 0846cc8 Compare September 19, 2021 17:58
@nicolas-grekas nicolas-grekas added this to the 5.4 milestone Sep 19, 2021
Copy link
Member

@yceruto yceruto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 just a minor inaccuracy about what choice_value callback should return (it's the string "value" of each choice)

Copy link
Contributor

@Tobion Tobion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very clean. Just missing a changelog entry.

Signed-off-by: Alexander M. Turek <me@derrabus.de>
@derrabus derrabus force-pushed the feature/form-enum-type branch from 0b5b2c6 to c6edc34 Compare September 21, 2021 06:25
@fabpot
Copy link
Member

fabpot commented Sep 21, 2021

Thank you @derrabus.

@rccc
Copy link

rccc commented Mar 27, 2025

Hello,

We cannot do this:

enum MoleculeFields: string { case Molecule Name = 'Molecule Name'; }

but we can do this

enum MoleculeFields: string { case Molecule_Name = 'Molecule Name'; }
Or even

enum MoleculeFields: string { case MoleculeName = 'Molecule Name'; }

But "molecule_name" or "v" will be display in the select field.

How wan we display "Molecue Name" while havinf "molecule_name" in the field value attribute ?

Eric

@xabbuh
Copy link
Member

xabbuh commented Mar 27, 2025

For support, please refer to one of the support channels. Thank you for understanding.

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

Successfully merging this pull request may close these issues.

9 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.