0
votes

I have an entity with ManyToMany field, rendered with EntityType and choices limited to existing links :

Sample of the entity :

/**
 * @var House[]|ArrayCollection
 *
 * @ORM\ManyToMany(targetEntity=House::class, fetch="LAZY")
 * @ORM\JoinTable(name="city_house")
 */
private $houses;

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


public function addHouse(House $house)
{
    if ($this->houses->contains($house)) {
        return $this;
    }

    $this->houses[] = $house;

    return $this;
}

public function removeHouse(House $house): bool
{
    if (!$this->houses->contains($house)) {
        return true;
    }

    return $this->houses->removeElement($house);
}

/**
 * Get brands.
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getBrands(): ?Collection
{
    return $this->brands;
}

And my formBuilder :

    $builder
        ->add('houseTypes', EntityType::class, [
            'multiple' => false,
            'required' => false,
            'mapped' => false,
            'class' => HouseType::class,
        ])
        ->add('houses', EntityType::class, [
            'multiple' => true,
            'required' => false,
            'class' => House::class,
            'by_reference' => false,
            'choices' => $builder->getData()->getHouses(),
        ])

I use multiselect js in order to add filtred houses (by type) in houses EntityType.

The JS part is working well, it adding options to select link this

        <option value="6">House (type)</option>

When i remove element form this list, symfony remove the link well. But when a add new element, the formValdator raise an error.

ConstraintViolation {#3985 ▼
  root: Form {#2783 …}
  path: "children[houses]"
  value: [▼
    "57"
    "3"
    "2"
  ]
}

TransformationFailedException {#2663 ▼
  #message: "Unable to reverse value for property path "houses": Could not find all matching choices for the given values"
  #code: 0
  #file: "/srv/vendor/symfony/form/Form.php"
  #line: 1115
  trace: {▶}
   …1
}

TransformationFailedException {#2678 ▼
  #message: "Could not find all matching choices for the given values"
  #code: 0
  #file: "/srv/vendor/symfony/form/Extension/Core/DataTransformer/ChoicesToValuesTransformer.php"
  #line: 68
  trace: {▶}
}

Do you have any idea of how to fix that ?

1

1 Answers

0
votes

I figure out how to do it, we need to use form events.

So, i added listener :

$builder->addEventListener(FormEvents::PRE_SET_DATA, [$this, 'onPreSetData']);
$builder->addEventListener(FormEvents::PRE_SUBMIT, [$this, 'onPreSubmit']);

Add the callback functions

/**
 * @param FormEvent $event
 */
public function onPreSubmit(FormEvent $event)
{
    $form = $event->getForm();
    $data = $event->getData();
    $brandsCollection = null;
    if (isset($data['houses'])) {
        $housesCollection = $this->em->getRepository(House::class)->findById($data['houses']);
    }

    $houses = new ArrayCollection($housesCollection ?? []);

    $this->addHouse($form, $houses);
}

/**
 * @param FormEvent $event
 */
public function onPreSetData(FormEvent $event)
{
    $promo = $event->getData();
    $form = $event->getForm();

    $houses = $promo->getHouses() ? $promo->getHouses() : null;

    $this->addHouse($form, $houses);
}

/**
 * @param FormInterface   $form
 * @param Collection|null $houses
 */
protected function addHouse(FormInterface $form, Collection $houses = null)
{
    $form->add('houses', EntityType::class, [
        'multiple' => true,
        'required' => false,
        'class' => House::class,
        'by_reference' => false,
        'choices' => $houses,
    ]);
}