-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Allow ChoiceType to select from tree-like structures #3835
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
+1 for the TreeChoiceList option - I posted about this almost a year ago #1333 |
To insert attributes in the options I have created a new type of widget and override theme of the block choice_widget_options. See the code used below. # Labone\Bundle\OrderBundle\Form\RequestProductType.php
...
class RequestProductType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('product', 'labone_entity_product', array(
'class' => "Labone\Bundle\ProductBundle\Entity\Product",
'attr' => array('class' => "chosen_find input-xlarge")
)) # Labone\Bundle\OrderBundle\Form\Type\LaboneEntityProductType.php
...
class LaboneEntityProductType extends AbstractType
{
public function getParent()
{
return 'entity';
}
public function getName()
{
return 'labone_entity_product';
}
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
foreach ($view->vars['choices'] as $choice) {
$choice->attr = array_replace(isset($choice->attr) ? $choice->attr : array(), array(
'data-price_unitary' => $choice->data->getPrice()
));
}
}
} # choice_widget_options.html.twig
{% block choice_widget_options %}
{% spaceless %}
{% for group_label, choice in options %}
{% if choice is iterable %}
<optgroup label="{{ group_label|trans({}, translation_domain) }}">
{% set options = choice %}
{{ block('choice_widget_options') }}
</optgroup>
{% else %}
<option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}
{% if choice.attr is defined %}
{% for attrname, attrvalue in choice.attr %}{% if attrname in ['title'] %}{{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}" {% else %}{{ attrname }}="{{ attrvalue }}" {% endif %}{% endfor %}
{% endif %}
>{{ choice.label|trans({}, translation_domain) }}</option>
{% endif %}
{% endfor %}
{% endspaceless %}
{% endblock choice_widget_options %} observer with the snippet code: {% for attrname, attrvalue in choice.attr %}...{% endfor %} and see change in base block form_div_layout.html.twig#L8. # Labone/Bundle/OrderBundle/Resources/cofig/webservice.yml
services:
labone_order.form.type.entity_product:
class: Labone\Bundle\OrderBundle\Form\Type\LaboneEntityProductType
tags:
- { name: form.type, alias: labone_entity_product } |
@webmozart what do you think about this feature request now ? |
👍 |
I have the same issue here but I need to show the form with the "expanded" + "multiple" option : using checkboxes. So I need a tree of checkboxes. In my case I cannot use "choice_widget_options" block. Any suggestion ? |
👍 |
i create this lazy FormType for this issue. my entities has parent and childs getters. class EntityTreeType extends AbstractType {
public function buildView( FormView $view , FormInterface $form , array $options ) {
$choices = [];
foreach ( $view->vars['choices'] as $choice ) {
$choices[] = $choice->data;
}
$choices = $this->buildTreeChoices( $choices );
$view->vars['choices'] = $choices;
}
protected function buildTreeChoices( $choices , $level = 0 ) {
$result = array();
foreach ( $choices as $choice ){
$result[] = new ChoiceView(
str_repeat( '-' , $level ) . ' ' . $choice->getName(),
$choice->getId(),
$choice,
[]
);
if ( !$choice->getChilds()->isEmpty() )
$result = array_merge(
$result,
$this->buildTreeChoices( $choice->getChilds() , $level + 1 )
);
}
return $result;
}
public function setDefaultOptions( OptionsResolverInterface $resolver ) {
$resolver->setDefaults(array(
'query_builder' => function ( EntityRepository $repo ) {
return $repo->createQueryBuilder('e')->where('e.parent IS NULL');
}
));
}
public function getParent() {
return 'entity';
}
/**
* Returns the name of this type.
*
* @return string The name of this type
*/
public function getName()
{
return 'entity_tree';
}
} View output like this for options :
|
I think the constructor of ChoiceView changed.
|
Closing this old (6 years old) feature request as nobody ever worked on an actual implementation. |
Sometimes it would be nice to allow the user to select from a tree structure, such as a
Implementation suggestion: a TreeChoiceList
Origin of this issue: #3456
The text was updated successfully, but these errors were encountered: