0

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

The code failing :

namespace App\Controller;

use App\Entity\Documents\ventehabitation;
use App\Form\Documents\ventehabitationType;
use App\Entity\UserDocuments;
use App\Entity\Documents\contratdetravail;
use App\Form\Documents\contratdetravailType;
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 = "App\\Entity\\Documents\\".$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 "App\Form\Documents\" 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/",
            "App\\Controller\\Documents\\": "src/Controller/Documents/",
            "App\\Entity\\Documents\\": "src/Entity/Documents/",
            "App\\Form\\Documents\\": "src/Form/Documents"
        }
    },

My class does exist. For example ventehabitationType form class is created in "App\Form\Documents\" and contains :

namespace App\Form\Documents;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use App\Entity\Documents\ventehabitation;

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

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            '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.

0

1 Answer 1

0

The ::class keyword will return a string with the fully qualified class name according to its namespace. However, it's a compile time operation so you cannoy use it at runtime.

Since it returns just a string and that's all the FormFactory expects, you can build it in the same manner as your $docEntity class name:

$formEntity = 'App\Form\Documents\\'.$docEntity.'Type';
2
  • Thanks a lot, it solved it. I didn't know this was a compile time operation, though it seemed obvious once I read your comment. Thanks again !
    – ww0k
    Oct 13, 2020 at 17:06
  • @ww0k Well, it's a keyword after all, so if you embed it in a string it won't be evaluated. Glad it's working.
    – msg
    Oct 13, 2020 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.