title: Symfony 6 populate Dropdown with Dynamic entities
body: <p>I have a CreditCard entity, on form screen there is a dropdown with user's Bank entities and I want to fill BankAccount dropdown according to selected bank dynamically, I have tried couple solutions from SO and added an event listener to my form but when I change the Bank dropdown I get &quot;Integrity constraint violation: kart_adi (credit card name field on entity) cannot be null. I am using Symfony 6 with php 8.1.2</p>
  <p>KrediKarti.php:</p>
  <pre><code>class KrediKarti
  {
  #[ORM\Id]
  #[ORM\GeneratedValue]
  #[ORM\Column(type: 'integer')]
  private $id;
  
  #[ORM\ManyToOne(targetEntity: Banka::class)]
  #[ORM\JoinColumn(nullable: false)]
  private $banka;
  
  #[ORM\ManyToOne(targetEntity: BankaHesap::class, inversedBy: 'krediKartlari')]
  #[ORM\JoinColumn(nullable: false)]
  private $hesap;
  
  #[ORM\Column(type: 'string', length: 255)]
  private $kart_adi;
  
  #[ORM\Column(type: 'string', length: 255)]
  private $kart_no;
  
  #[ORM\Column(type: 'integer')]
  private $hesap_kesim_gunu;
  
  #[ORM\Column(type: 'string', length: 255, nullable: true)]
  private $aciklama;
  
  #[ORM\Column(type: 'float')]
  private $borc;
  
  #[ORM\Column(type: 'float')]
  private $alacak;
  
  #[ORM\Column(type: 'datetime_immutable')]
  private $created_at;
  
  public function getId(): ?int
  {
      return $this-&gt;id;
  }
  
  public function getBanka(): ?Banka
  {
      return $this-&gt;banka;
  }
  
  public function setBanka(?Banka $banka): self
  {
      $this-&gt;banka = $banka;
  
      return $this;
  }
  
  public function getHesap(): ?BankaHesap
  {
      return $this-&gt;hesap;
  }
  
  public function setHesap(?BankaHesap $hesap): self
  {
      $this-&gt;hesap = $hesap;
  
      return $this;
  }
  
  public function getKartAdi(): ?string
  {
      return $this-&gt;kart_adi;
  }
  
  public function setKartAdi(string $kart_adi): self
  {
      $this-&gt;kart_adi = $kart_adi;
  
      return $this;
  }
  
  public function getKartNo(): ?string
  {
      return $this-&gt;kart_no;
  }
  
  public function setKartNo(string $kart_no): self
  {
      $this-&gt;kart_no = $kart_no;
  
      return $this;
  }
  
  public function getHesapKesimGunu(): ?int
  {
      return $this-&gt;hesap_kesim_gunu;
  }
  
  public function setHesapKesimGunu(int $hesap_kesim_gunu): self
  {
      $this-&gt;hesap_kesim_gunu = $hesap_kesim_gunu;
  
      return $this;
  }
  
  public function getAciklama(): ?string
  {
      return $this-&gt;aciklama;
  }
  
  public function setAciklama(?string $aciklama): self
  {
      $this-&gt;aciklama = $aciklama;
  
      return $this;
  }
  
  public function getBorc(): ?float
  {
      return $this-&gt;borc;
  }
  
  public function setBorc(float $borc): self
  {
      $this-&gt;borc = $borc;
  
      return $this;
  }
  
  public function getAlacak(): ?float
  {
      return $this-&gt;alacak;
  }
  
  public function setAlacak(float $alacak): self
  {
      $this-&gt;alacak = $alacak;
  
      return $this;
  }
  
  public function getCreatedAt(): ?\DateTimeImmutable
  {
      return $this-&gt;created_at;
  }
  
  public function setCreatedAt(\DateTimeImmutable $created_at): self
  {
      $this-&gt;created_at = $created_at;
  
      return $this;
  }
  }
  </code></pre>
  <p>KrediKartiType.php:</p>
  <pre><code>$builder
          -&gt;add('kart_adi',TextType::class,['label' =&gt; 'Kart Adı','required' =&gt; false, 'attr' =&gt;['class' =&gt; 'form-control', 'placeholder' =&gt; 'Kart Adı']])
          -&gt;add('kart_no',TextType::class,['label' =&gt; 'Kart No','required' =&gt; false, 'attr' =&gt;['class' =&gt; 'form-control', 'placeholder' =&gt; 'Kart No']])
          -&gt;add('hesap_kesim_gunu', NumberType::class, [
              'label' =&gt; 'Hesap Kesim Tarihi', 'attr' =&gt;['class' =&gt; 'form-control', 'placeholder' =&gt; 'Hesap Kesim Günü']
          ])
          -&gt;add('aciklama',TextType::class,['label' =&gt; 'Açıklama','required' =&gt; false, 'attr' =&gt;['class' =&gt; 'form-control', 'placeholder' =&gt; 'Açıklama']])
          -&gt;add('borc', NumberType::class,
              [
                  'label' =&gt; 'Borç', 'attr' =&gt;['class' =&gt; 'form-control', 'placeholder' =&gt; &quot;Borç&quot;,'value' =&gt; '0.00','style' =&gt; 'text-align: right']
              ])
          -&gt;add('alacak', NumberType::class,
              [
                  'label' =&gt; 'Alacak', 'attr' =&gt;['class' =&gt; 'form-control', 'placeholder' =&gt; &quot;Alacak&quot;,'value' =&gt; '0.00','style' =&gt; 'text-align: right']
              ])
          -&gt;add('banka',  EntityType::class, array(
              'class' =&gt; Banka::class,
              'choice_label'=&gt; 'adi',
              'placeholder' =&gt; 'Lütfen bankanızı seçiniz',
              'attr' =&gt;['class' =&gt; 'form-control']
          ))
  
      ;
  
      $formModifier = function (FormInterface $form, Banka $banka = null) {
          $banka_hesap = null === $banka ? [] : $banka-&gt;getBankaHesaplari();
  
          $form-&gt;add('hesap', EntityType::class, [
              'class' =&gt; BankaHesap::class,
              'placeholder' =&gt; '',
              'choices' =&gt; $banka_hesap,
          ]);
      };
  
      $builder-&gt;addEventListener(
          FormEvents::PRE_SET_DATA,
          function (FormEvent $event) use ($formModifier) {
              $data = $event-&gt;getData();
  
              $formModifier($event-&gt;getForm(), $data-&gt;getBanka());
          }
      );
  
      $builder-&gt;get('banka')-&gt;addEventListener(
          FormEvents::POST_SUBMIT,
          function (FormEvent $event) use ($formModifier) {
              $banka = $event-&gt;getForm()-&gt;getData();
  
              $formModifier($event-&gt;getForm()-&gt;getParent(), $banka);
          }
      );
  </code></pre>
  <p>new.html.twig:</p>
  <pre><code>&lt;script&gt;
  var $banka = $('#kredi_karti_banka');
  var $token = $('#kredi_karti__token');
  $banka.change(function() {
      var $form = $(this).closest('form');
      var data = {};
      data[$banka.attr('name')] = $banka.val();
      data[$token.attr('name')] = $token.val();
      $.ajax({
      url : $form.attr('action'),
      type: $form.attr('method'),
      data : data,
      complete: function(html) {
      // Replace current position field ...
      $('#kredi_karti_hesap').replaceWith(
      // ... with the returned one from the AJAX response.
      $(html.responseText).find('#meetup_position')
      );
      // Position field now displays the appropriate positions.
      }
      });
  });
  &lt;/script&gt;
  </code></pre>
  <p>I simply want to fetch bank accounts based on the selected bank on bank change.</p>