Skip to content

Symfony Form Component: ChoiceType: add possibility for placeholder to be disabled #22318

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
elHornair opened this issue Apr 6, 2017 · 10 comments

Comments

@elHornair
Copy link

Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? yes
Symfony version 3.2

Currently, the placeholder of the Symfony Form components ChoiceType doesn't allow to set the rendered option in the HTML select to disabled. This is a use case however, because we don't always want the placeholder to be selectable (probably on the contrary). I see two ways how this could be fixed:

  1. Add a new option palceholder_attributes to https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php (then we could manually set disabled to the value we want and respect it in the template).
  2. Use the already existing template parameter placeholder_in_choices in the template. If the placeholder is not in the choices, we set the rendered placeholder option in the HTML select to disabled.

I can go ahead and create a PR, but I first wanted to hear your thoughts about it. Personally, I think option 2 is better. Wdyt?

@elHornair
Copy link
Author

Any news here? Could someone please say "yay" or "nay" so I can go ahead and implement it or drop the issue?

@HeahDude
Copy link
Contributor

Hello @elHornair, thank you for opening this issue.

I don't really understand the need to set the placeholder disabled, because in cases where the choice is single (expanded or not), we have to be able to select it to unselect the other choices.

Such feature concerns only multiple choices, but placeholder is not used for multiple choices (ref https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php#L295).

Anyway, in very specific cases like that, I would go by setting the placeholder option from the type to false and adding a disabled input hardcoded in a theme.

If you really need wide support in your app before it's in the core, you can easily create an extension for the ChoiceType to add an option, let's say disable_placeholder, and hook in buildForm() to add something like:

if ($options['expanded'] && options['disable_placeholder'] && $builder->has('placeholder')) {
    $builder->get('placeholder')->setDisabled(true);
}

I'm not sure that handling this globally in the framework is worth it so I'm waiting for more feedback.

@elHornair
Copy link
Author

Thanks for your reply @HeahDude.

we have to be able to select it to unselect the other choices

That's the thing: if placeholder_in_choices is false, we don't want the user to unselect their choice, because this would lead to a validation error anyway (thus, my suggestion #2).

Also, thanks for your two suggestion - Both of these would work for me as a workaround. However I'd rather fix it in the core as others might benefit from it as well

@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@carsonbot
Copy link

Hello? This issue is about to be closed if nobody replies.

@xabbuh
Copy link
Member

xabbuh commented Jan 3, 2021

I think adding something like a placeholder_attr option might be possible. @elHornair Would that satisfy your needs?

@carsonbot carsonbot removed the Stalled label Jan 3, 2021
@elHornair
Copy link
Author

@xabbuh Hi! Sorry, I'm not actively using Symfony anymore and I don't remember what this issue was originally about. As far as I'm concerned the issue can either be closed or fixed in any way you see fit :)

@xabbuh
Copy link
Member

xabbuh commented Jan 8, 2021

Thank you for the feedback. I am closing here then. We can consider to reopen if someone runs into it again in the future.

@xabbuh xabbuh closed this as completed Jan 8, 2021
@devsigner-xyz
Copy link

Hello, for me it also would be a useful feature. The placeholder_attr option sounds like a great idea.
Like @elHornair said it would be useful when user has selected a choice and you don't want the user unselect it.

@shanereichart
Copy link

I found this while trying to figure out why placeholders weren't disabled by default when the select field is required. I'm surprised to discover not only is it by design, but so few people are inquiring about it. I'd go for the placeholder_attr solution, but really don't understand why you would ever want a selectable placeholder option if the select field is required, so I'd be ever more in favor of the option being disabled by default under those circumstances.

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

No branches or pull requests

6 participants