Search code examples
symfonyoopentityformbuilder

How can I load form options from Doctrine entity (Symfony 4)?


I am trying to load my formbuilder options from my Doctrine entity FieldTypes:

$formBuilder->add('type', EntityType::class, array(
          'attr' => array('class' => 'form-control select2'), 'label' => 'Type',
          'class' => FieldTypes::class,
        ));

But I get the error message:

Class App\Controller\FieldTypes does not exist


Solution

  • you should put a use at the top of class:

    namespace App\Controller;
    
    use App\Entity\FieldTypes;
    
    class MyController
    {
        // ...
    
        public function myAction()
        {
            // ...
    
            $formBuilder->add('type', EntityType::class, array(
                'attr' => array(
                    'class' => 'form-control select2'
                ),
                'label' => 'Type',
                'class' => FieldTypes::class,
            ));
    
            // ...
        }
    
        // ...
    }