title: How do I create a form in Symfony 5 for entity with relations to other entities with have other relations in Symfony way?
body: <p>Hi all and thanks in advance for any help!
  Let's say I have a Symfony <strong>5</strong> App with have entities like:
  Hero, Profession, ArmorGroup &amp; Armor and WeaponGroup &amp; Weapon.
  The relation between them is like this:</p>
  <ul>
  <li>Single unique <strong>Hero</strong> is a set of one <strong>Profession</strong>, one <strong>Armor</strong> and one
  <strong>Weapon</strong></li>
  <li>Single unique <strong>Profession</strong> can contain many <strong>Heroes</strong>, many <strong>ArmorGroups</strong> and many <strong>WeaponGroups</strong></li>
  <li>Single unique <strong>ArmorGroup</strong> is a collection of many <strong>Armors</strong></li>
  <li>Single unique <strong>WeaponGroup</strong> is a collection of many <strong>Weapons</strong></li>
  <li>Single unique <strong>Armor</strong> is in a collection of one <strong>ArmorGroups</strong> and is dedicated to one <strong>Profession</strong></li>
  <li>Single unique <strong>Weapon</strong> is in a collection of one <strong>WeaponGroups</strong> and is dedicated to one <strong>Profession</strong></li>
  </ul>
  <p>Here is a diagram:
  <a href="https://i.stack.imgur.com/H3dP6.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/H3dP6.jpg" alt="enter image description here" /></a></p>
  <p>I want to create a <strong>HeroType</strong> form that limits selection of <strong>Armor</strong> and <strong>Weapon</strong> to previously selected <strong>Profession</strong> and show them as a <strong>EntityType</strong> single-choice list with names based on data from <strong>ArmorGroups</strong> for <strong>Armor</strong> and <strong>WeaponGroups</strong> for <strong>Weapon</strong>. All classes have many unique parameters.</p>
  <p>Here is my way that is not working:</p>
  <pre><code>&lt;?php
  class HeroType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('name', NumberType::class, [
              'attr' =&gt; ['placeholder' =&gt; 'John Doe'],
              'label' =&gt; 'Hero name.',
              'required' =&gt; true,
              ])
              -&gt;add('profession', EntityType::class, [
              'choice_label' =&gt; 'name',
              'class' =&gt; Profession::class,
              'group_by' =&gt; 'affiliation',
              'label' =&gt; 'Profession',
              'multiple' =&gt; false,
              'required' =&gt; false,
              ])
              -&gt;add('item_armor', EntityType::class, [
              'choice_label' =&gt; 'model',
              'class' =&gt; ItemArmor::class,
              'group_by' =&gt; 'material',
              'label' =&gt; 'Armor',
              'multiple' =&gt; false,
              'required' =&gt; false,
              ])
              -&gt;add('item_weapon', EntityType::class, [
              'choice_label' =&gt; 'model',
              'class' =&gt; ItemWeapon::class,
              'group_by' =&gt; 'material',
              'label' =&gt; 'Weapon',
              'multiple' =&gt; false,
              'required' =&gt; false,
              ])
              -&gt;add('save', SubmitType::class, ['label' =&gt; 'Create Hero!']);
      }
  
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults([
              'data_class' =&gt; Hero::class,
              ''
          ]);
      }
  }
  </code></pre>
  <p>It can't get property for <strong>'choice_label'</strong> and <strong>'group_by'</strong>. I want to make a form for  <strong>Hero</strong> that fetch data from <strong>ArmorGroups/WeaponGroups</strong> to use as labels, but the values should be <strong>Armor/Weapon</strong> ID's.</p>
  <p><strong>UPDATE:</strong>
  This code render the form correctly and as expected:</p>
  <pre><code>&lt;?php
  class HeroType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('name', NumberType::class, [
              'attr' =&gt; ['placeholder' =&gt; 'John Doe'],
              'label' =&gt; 'Hero name.',
              'required' =&gt; true,
              ])
              -&gt;add('profession', EntityType::class, [
              'choice_label' =&gt; function ($profession) {
                  return $profession-&gt;getName();},
              'group_by' =&gt; function ($profession) {
                  return $profession-&gt;getAffiliation();},
              'class' =&gt; Profession::class,
              'label' =&gt; 'Profession',
              'multiple' =&gt; false,
              'required' =&gt; false,
              ])
              -&gt;add('item_armor', EntityType::class, [
              'choice_label' =&gt; function ($item_armor) {
                  return $item_armor-&gt;getIdItemArmor()-&gt;getModel();},
              'group_by' =&gt; function ($item_armor) {
                  return $item_armor-&gt;getIdItemArmor()-&gt;getMaterial();},
              'class' =&gt; ItemArmor::class,
              'label' =&gt; 'Armor',
              'multiple' =&gt; false,
              'required' =&gt; false,
              ])
              -&gt;add('item_weapon', EntityType::class, [
              'choice_label' =&gt; function ($item_weapon) {
                  return $item_weapon-&gt;getIdItemWeapon()-&gt;getModel();},
              'group_by' =&gt; function ($item_weapon) {
                  return $item_weapon-&gt;getIdItemWeapon()-&gt;getMaterial();},
              'class' =&gt; ItemWeapon::class,
              'label' =&gt; 'Weapon',
              'multiple' =&gt; false,
              'required' =&gt; false,
              ])
              -&gt;add('save', SubmitType::class, ['label' =&gt; 'Create Hero!']);
      }
  
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults([
              'data_class' =&gt; Hero::class,
              ''
          ]);
      }
  }
  </code></pre>
  <p>But now I get error:</p>
  <blockquote>
  <p>Expected argument of type &quot;App\Entity\Profession or null&quot;, &quot;instance of Doctrine\Common\Collections\ArrayCollection&quot; given at property path &quot;profession&quot;.</p>
  </blockquote>