-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form]: Display message next to indivdual radio buttons #36484
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
Comments
Hello @ThomasLandauer, you can already achieve this by implementing the $view['gender']['radio_name']['foo'] = 'bar'; to modify their corresponding |
@HeahDude cool, thanks!! :-) I got it to work with this inside $view['gender'][0]->vars['foo'] = 'bar'; or more verbose: $view->children['gender']->children[0]->vars['foo'] = 'bar'; Your exact code didn't work for me: $view['gender']->vars['choices'][0]['foo'] ... I'm getting
... which is reasonable - see https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/Form/ChoiceList/View/ChoiceView.php And your line Just asking to make sure I didn't miss anything. Anyway, I'm leaving this issue open, as I see this more as a workaround than a clean solution: Since the choices are addressed by index, it might break when they get reordered. What about explaining this in the docs? Radio buttons are a special case to me, since they're the only widget where you can't just add something in the template, since you cannot address them individually. So what about adding a dedicated page to https://symfony.com/doc/current/forms.html#learn-more ? |
Thanks for the feedback :).
Both are resolved the same, by letting original choices keys be the name by default or incremented value as fallback. To get control you can use the In your case $view->children['gender'][0]->vars['foo'] = 'bar'; since the So I don't see for now what could be improved, IMO the
Yes I agree on this point :), feel free to submit something, though I think it should be done (briefly) in the main Form guide, with a link to the custom field article you mentioned which is going to be updated a little already (ref symfony/symfony-docs#13488 and symfony/symfony-docs#13490). Maybe you can see something that would fit well on top of that? I would close here nevertheless, unless you have an example in mind of what could be achieved. |
You're talking about this https://symfony.com/doc/current/reference/forms/types/choice.html#advanced-example-with-objects , right? I wasn't aware of this, so I'm closing the issue. Docs: I just can't work with this GitHub preview, so I'm gonna wait till your PR's are merged :-) |
Description
I'm looking for a way to display a message next to the label of individual radio buttons (RadioType, i.e. ChoiceType expanded). I think this is the only type of widgets for which Symfony doesn't yet offer a nice way to customize their rendering.
Here's my idea:
Since there are 100 ways of how/where to exactly display that "message" (info, data, whatever), a one-size-fits-all approach probably won't work. So the users will have to adapt their form theme in any case. The question is: How to pass the data to the form template?
Status quo:
If you loop over the field's choices in Twig, you can pass an arbitrary field name:
...and then take care of this
foo
field in your form theme.Proposed solution:
Allow adding custom data to the
choices
array in the FormType. How could this be done? Right now, choices is an array in the form [label=>value]. Suggestion:This would give the user a clean way to pass information to the form theme. The actual rendering is left to the theme.
The text was updated successfully, but these errors were encountered: