title: Symfony form: filter collection with a parameter
body: <p>I am using Symfony 3.4.</p>
  
  <p>I have a form to edit some answers for a given country and I would need to edit these answers on a per category basis.</p>
  
  <p>I have setup a filter in my entity but I don't know how to call this getter with a parameter from my form.</p>
  
  <p>The dirty alternative would be to remove non required elements from my form in my controller but this is not ideal...</p>
  
  <p>Here is my form:</p>
  
  <pre><code>&lt;?php
  
  namespace cwt\psmdbBundle\Form;
  
  use cwt\psmdbBundle\Entity\CcdbServicesAnswers;
  use cwt\psmdbBundle\Entity\countries;
  
  use Doctrine\ORM\EntityRepository;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  
  class countriesCCDBServicesAnswersType extends AbstractType
  {
  
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('AnswersOfCategory', CollectionType::class, array(
                  'entry_type' =&gt; CcdbServicesAnswersType::class,
                  'entry_options' =&gt; array('label' =&gt; false,),
                  'label' =&gt; false,
              ));    
      }
  
      /**
       * @param OptionsResolver $resolver
       */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'cwt\psmdbBundle\Entity\countries',
              'categoryID' =&gt; 1,
          ));
      }
  
      /**
       * @return string
       */
      public function getBlockPrefix()
      {
          return 'cwt_psmdbbundle_countries';
      }
  }
  </code></pre>
  
  <p>Here is my entity:</p>
  
  <pre><code>&lt;?php
  
  namespace cwt\psmdbBundle\Entity;
  
  use APY\DataGridBundle\Grid\Mapping as GRID;
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * countries
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="cwt\psmdbBundle\Entity\Repository\countriesRepository")
   */
  class countries
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="code", type="string", length=2)
       */
      private $code;
  
      /**
       * @var string
       *
       * @ORM\Column(name="name", type="string", length=100)
       */
      private $name;    
  
      /**
       * @ORM\OneToMany(targetEntity="cwt\psmdbBundle\Entity\CcdbServicesAnswers", mappedBy="country", cascade={"persist", "remove"})
       */
      private $ccdbServicesAnswers;
  
  
  // Specific Functions
  
      /**
       * Get ccdbServicesAnswers.
       *
       * @return \Doctrine\Common\Collections\Collection
       */
      public function getAnswersOfCategory($categoryID)
      {
  
          return $this-&gt;getCcdbServicesAnswers()-&gt;filter(function(CcdbServicesAnswers $answer) use($categoryID) {
              return $answer-&gt;getQuestion()-&gt;getCategory()-&gt;getId() == $categoryID;
          });
      }
  </code></pre>
  
  <p>And here is my create form function in my controller:</p>
  
  <pre><code>private function createCountryEditForm(Countries $entity, $categoryID)
  {
      $form = $this-&gt;createForm(countriesCCDBServicesAnswersType::class, $entity, array(
          'action' =&gt; $this-&gt;generateUrl('updateCountryCcdbServicesAnswers', array('countryID' =&gt; $entity-&gt;getId())),
          'method' =&gt; 'PUT',
          'categoryID' =&gt; $categoryID,
      ));
  
      $form-&gt;add('submit', submitType::class, array('label' =&gt; 'Update', 'attr' =&gt; array('class' =&gt; 'hidden')));
  
      return $form;
  }
  </code></pre>
  
  <p><strong>[UPDATE 20/11/2018]</strong></p>
  
  <p>Here is what I did so far. This work but I would prefer a cleaner solution which deals with this filter with a where clause at database level and which would be more DRY. Doing this at the conroller level means that I have to do it everywhere I need this filter.</p>
  
  <pre><code>private function createCountryEditForm(Countries $entity, $categoryID)
  {
      $form = $this-&gt;createForm(countriesCCDBServicesAnswersType::class, $entity, array(
          'action' =&gt; $this-&gt;generateUrl('updateCountryCcdbServicesAnswers', array('countryID' =&gt; $entity-&gt;getId(), 'categoryID' =&gt; $categoryID)),
          'method' =&gt; 'PUT',
          'categoryID' =&gt; $categoryID,
      ));
  
      $answers = $form-&gt;get('ccdbServicesAnswers')-&gt;getData();
  
  
      foreach ($answers as $answer) {
  
          if($answer-&gt;getQuestion()-&gt;getCategory()-&gt;getId() != $categoryID) {
              $answers-&gt;removeElement($answer);
          }
  
      }
  
      $form-&gt;get('ccdbServicesAnswers')-&gt;setData($answers);
  
      $form-&gt;add('submit', submitType::class, array('label' =&gt; 'Update', 'attr' =&gt; array('class' =&gt; 'hidden')));
  
      return $form;
  }
  </code></pre>