symfony createForm dynamic parameters error

I’m encountering a strange problem when calling create form method in symfony 5.1.

The code failing :

namespace AppController;

use AppEntityDocumentsventehabitation;
use AppFormDocumentsventehabitationType;
use AppEntityUserDocuments;
use AppEntityDocumentscontratdetravail;
use AppFormDocumentscontratdetravailType;
public function fillDocument(DocumentsRepository $docRepo, Request $request, int $id)
            $currentDoc = $docRepo->findBy(["id" => $id]);
            $name = $currentDoc[0]->getDocModelName();
            $docEntity= str_replace("_", "", $name);
            $formEntity = $docEntity."Type::class";
            $docEntity = "AppEntityDocuments".$docEntity;

            $document = new $docEntity();

            $form = $this->createForm($formEntity, $document, [], "");

Error : Could not load type "ventehabitationType::class": class does not exist.

Same error if I prepend "AppFormDocuments" to the $formEntity variable.
But working fine If I explicly write ventehabitationType::class in createForm parameters, which isn’t helping me in this case since I need to initiate dynamically my forms.

composer.json infos in case you would need it :

    "autoload": {
        "psr-4": {
            "App": "src/",
            "AppControllerDocuments": "src/Controller/Documents/",
            "AppEntityDocuments": "src/Entity/Documents/",
            "AppFormDocuments": "src/Form/Documents"

My class does exist. For example ventehabitationType form class is created in "AppFormDocuments" and contains :

namespace AppFormDocuments;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeDateTimeType;
use SymfonyComponentFormExtensionCoreTypeSubmitType;
use SymfonyComponentFormExtensionCoreTypeTextareaType;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use AppEntityDocumentsventehabitation;

class ventehabitationType extends AbstractType
    public function buildForm(FormBuilderInterface $builder, array $options): void
            ->add('test', TextType::class, [
                'label' => 'test',

    public function configureOptions(OptionsResolver $resolver)
            'data_class' => ventehabitation::class,

My entity is loaded correctly, only the first parameter of createForm is the problem.

This seems to be such a noobie problem but still I can’t figure it out…
Maybe it is a PHP restriction ?

If someone can solve the problem, please let me know.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

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