0

I'm using Symfony 6 to filter an entity in a form type. My problem is 'choice_filter' , I understand documentation, I test with simple application article and category and I want a select with not all category. I would like one or more category in select. In follow my last test with only one in select.

Error: Could not load type "App\Form\ChoiceType": class does not exist. In my controller:

$article = new Article();
$form = $this->createForm(ArticleType::class, $article);

ArticleTYpe:

public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
         ->add('category', EntityType::class, [
            'class' => Category::class,
            'choice_label' => 'name category',
            'choice_filter' => ChoiceList::filter(
                $this,
                function ($category) {
                    if ($category instanceof Category) {
                        return $category->getId(1);
                    }
                    return false;
                }
            )
        ])
            ;
    }
  • I don't see the `ChoiceType` in the code you've posted, but judging by the error you're probably missing a use statement for the `ChoiceType` somewhere: `use Symfony\Component\Form\Extension\Core\Type\ChoiceType;` – Marleen Sep 12 '22 at 10:03
  • I add ! and I have a pb with choice_label. now no error. I add also: return $category->getID() == 2 OR $category->getID() == 3; and now all is correct. But last question, is it possible add an array for more category ? – user331157 Sep 12 '22 at 19:09
  • Not sure if I understood your last question correctly, but if you want to use an array with ids of categories that should be displayed, you could use something like `return in_array($category->getID(), [2, 3, ...]);` – Marleen Sep 12 '22 at 19:38

0 Answers0