Expected type ‘SymfonyComponentFormFormInterface’. Found ‘null’

I want to add a field "subCategories" in a form wich should appear when a Category is selected.

Here is my formType:

<?php

namespace AppForm;

use AppEntityCategories;
use AppEntityKeywords;
use DoctrineORMEntityRepository;
use SymfonyBridgeDoctrineFormTypeEntityType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentFormFormEvent;
use SymfonyComponentFormFormEvents;
use SymfonyComponentFormFormInterface;
use SymfonyComponentOptionsResolverOptionsResolver;

class ArticlesSearchType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('category', EntityType::class, [
                'class' => Categories::class,
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('c')
                        ->andWhere('c.parent is NULL')
                        ->orderBy('c.name', 'ASC');
                },
                'choice_label' => 'name',
                'label' => false,
                'placeholder' => 'Recherche par catégorie',
                'required' => false
            ]);           

        $formModifier = function (FormInterface $form, Categories $categories = null) {
            $subCategories = null === $categories ? [] : $categories->getSubCategories();

            $form->add('subCategories', EntityType::class, [
                'class' => Categories::class,
                'choices' => $subCategories,
                'required' => false,
                'choice_label' => 'name',
                'placeholder' => 'Sous-catégorie (Choisir une catégorie)',
                'label' => 'Sous-catégorie'
            ]);
        };

        $builder->get('category')->addEventListener(
            FormEvents::POST_SUBMIT,
            function (FormEvent $event) use ($formModifier) {
                $categories = $event->getForm()->getData();
                $formModifier($event->getForm()->getParent(), $categories);
            }
        );
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'method' => 'get',
            'csrf_protection' => false
        ]);
    }

    public function getBlockPrefix()
    {
        return '';
    }
}

This line

$event->getForm()->getParent()

displays the following error :
"Expected type ‘SymfonyComponentFormFormInterface’. Found ‘null’."

This is my Categories entity configuration:

    /**
     * @ORMManyToOne(targetEntity=Categories::class, inversedBy="subCategories", cascade={"persist"})
     */
    private $parent;

    /**
     * @ORMOneToMany(targetEntity=Categories::class, mappedBy="parent", cascade={"persist"})
     */
    private $subCategories;

    public function getParent(): ?self
    {
        return $this->parent;
    }

    public function setParent(?self $parent): self
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * @return Collection|self[]
     */
    public function getSubCategories(): Collection
    {
        return $this->subCategories;
    }

    public function addCategory(self $category): self
    {
        if (!$this->subCategories->contains($category)) {
            $this->subCategories[] = $category;
            $category->setParent($this);
        }

        return $this;
    }

Finally this is how it looks in my database :
Categories table

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *