mercredi 29 juin 2016

Checkbox preselection is not working on EntityType Field (ManyToMany)

My Symfony version is 2.7.3. I have Vehicle entity, which has WorldCountry field (unidirectional ManyToMany):

/**
 * Vehicle
 * @ORMTable(name="vehicle")
 */
class Vehicle
{
    // ...

    /**
     * @ORMManyToMany(targetEntity="AppBundleEntityWorldCountry", fetch="EAGER")
     * @ORMJoinTable(name="vehicle_country_join",
     *   joinColumns={@ORMJoinColumn(name="vehicle_id", referencedColumnName="vehicle_id", onDelete="CASCADE")},
     *   inverseJoinColumns={@ORMJoinColumn(name="country_id", referencedColumnName="country_id")}
     * )
     */
    protected $countries;

    // ...

    public function __construct()
    {
        // ...
        $this->countries = new ArrayCollection();
    }
}

I have form for vehicle creation/editing:

/**
 * Class VehicleType
 * @package AppBundleFormVehicle
 */
class VehicleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            // ...
            ->add('countries', 'entity', [
                'class' => 'AppBundle:WorldCountry',
                'choices' => $this->worldManager->getCountriesByRegionIds([1]),
                'choice_label' => 'name',
                'expanded' => false,
                'multiple' => true,
                'label' => false
            ])
            ->add('submit', 'submit');

        // ...
    }
}

Vehicle creation works fine, for example as result I have: enter image description here

The problem is that on vehicle editing page countries are not preselected (“Kyrgyzstan” in that example), when “choices” option is used:

enter image description here

But without it countries are preselected:

enter image description here

$this->worldManager->getCountriesByRegionIds([1]) - returns array of WorldCountry from repository.

P.S. Please let me know if more information is required.

Aucun commentaire:

Enregistrer un commentaire