Last active
May 14, 2023 10:47
-
-
Save jaytaph/121246e3a22338a9f4638cfa20db2e73 to your computer and use it in GitHub Desktop.
Choice Type with help texts per option
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Form; | |
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | |
use Symfony\Component\Form\FormInterface; | |
use Symfony\Component\Form\FormView; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
class ChoiceTypeWithAttr extends ChoiceType | |
{ | |
public function configureOptions(OptionsResolver $resolver) | |
{ | |
parent::configureOptions($resolver); | |
$resolver->setDefined(['choice_help_labels']); | |
$resolver->setAllowedTypes('choice_help_labels', 'array'); | |
} | |
public function finishView(FormView $view, FormInterface $form, array $options) | |
{ | |
parent::finishView($view, $form, $options); | |
foreach ($view->children as $child) { | |
$child->vars['help'] = $options['choice_help_labels'][$child->vars['value']] ?? []; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'bootstrap_5_horizontal_layout.html.twig' %} | |
{%- block choice_widget_expanded -%} | |
<div {{ block('widget_container_attributes') }}> | |
{%- for child in form %} | |
{{- form_widget(child, { | |
parent_label_class: label_attr.class|default(''), | |
translation_domain: choice_translation_domain, | |
valid: valid, | |
}) -}} | |
{% if child.vars.help is defined %} | |
<div class="form-text">{{ child.vars.help }}</div> | |
{% endif %} | |
{% endfor -%} | |
</div> | |
{%- endblock choice_widget_expanded -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace App\Form; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | |
use Symfony\Component\Form\Extension\Core\Type\SubmitType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
class MyFormType extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options): void | |
{ | |
$builder | |
->add('roles', ChoiceTypeWithAttr::class, [ | |
'choices' => [ | |
'Label for foo' => 'foo', | |
'Label for bar' => 'bar', | |
'Label for baz' => 'baz', | |
], | |
'choice_help_labels' => [ | |
'foo' => 'Helptext for foo', | |
'bar' => 'Another helptext, but this is for bar', | |
'baz' => 'Final help text for baz', | |
], | |
'multiple' => true, | |
'expanded' => true, | |
]); | |
.... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment