Skip to content

[Form] Wrong entity choices rendered with groups since 2.7 #14915

Closed
@soullivaneuh

Description

@soullivaneuh

With this sample of code:

$productsChoices = $this->getDoctrine()->getRepository('AppBundle:Product')->getChoicesGroupByCategory();
dump($productsChoices);

$form = $this->createFormBuilder()
    ->add('product', 'entity', [
        'class'     => 'AppBundle\Entity\Product',
        'choices'       => $productsChoices,
        'property'      => 'nameWithPrice',
    ])
    ->getForm()
;

And an extract of dump($productsChoices);:

array:23 [▼
  "Adresses IP" => array:1 [▼
    0 => Product {#3546 ▼
      -id: 12
      -name: "Adresse IP Failover"
      -ref: "xxx"
      -price: 42.0
      -isRecurrent: true
      -category: ProductCategory {#3641 ▶}
      -features: PersistentCollection {#3561 ▶}
    }
  ]
  "Bande-Passante" => array:3 [▼
    0 => Product {#3660 ▼
      -id: 65
      -name: "Bande passante"
      -ref: "xxx"
      -price: 42.0
      -isRecurrent: true
      -category: ProductCategory {#3663 ▶}
      -features: PersistentCollection {#3661 ▶}
    }
    1 => Product {#3668 ▶}
    2 => Product {#3670 ▶}
  ]
  "CDN" => array:3 [▼
    0 => Product {#3674 ▼
      -id: 24
      -name: "CDN Statique"
      -ref: "xxx"
      -price: 42.0
      -isRecurrent: true
      -category: ProductCategory {#3677 ▶}
      -features: PersistentCollection {#3675 ▶}
    }
    1 => Product {#3681 ▶}
    2 => Product {#3684 ▶}
  ]

Twig rendering:

{{ form(form) }}

I got this rendered select:

<select id="form_product" name="form[product]" class=" form-control">
    <optgroup label="Adresses IP">
        <option value="18">VPN IpSec - 42.00 €</option>
    </optgroup>
    <optgroup label="Bande-Passante">
        <option value="18">VPN IpSec - 42.00 €</option>
        <option value="59">Ajout de disque - 42.00 €</option>
        <option value="58">Ajout de RAM - 42.00 €</option>
    </optgroup>
    <optgroup label="CDN">
        <option value="18">VPN IpSec - 42.00 €</option>
        <option value="59">Ajout de disque - 42.00 €</option>
        <option value="58">Ajout de RAM - 42.00 €</option>
    </optgroup>
    <!-- ... -->
</select>

For example, first value should not be "VPN IpSec" but "Adresse IP Failover".

This is a BC break and/or regressions because code was not modified and worked like a charm on 2.6.

Using last dev-master Symfony version:

$ composer info -i | grep symfony/symfony
symfony/symfony                          2.7.x-dev e09874b   The Symfony PHP framework

Same error on 2.7.0 stable.

Regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions