I'm using Symfony forms (v3.0) without the rest of the Symfony framework. Using Doctrine v2.5.
I've created a form, here's the form type class:
class CreateMyEntityForm extends BaseFormType {
public function buildForm(FormBuilderInterface $builder, array $options){
$builder->add('myEntity', EntityType::class);
}
}
When loading the page, I get the following error.
Argument 1 passed to Symfony\Bridge\Doctrine\Form\Type\DoctrineType::__construct() must be an instance of Doctrine\Common\Persistence\ManagerRegistry, none given, called in /var/www/dev3/Vendor/symfony/form/FormRegistry.php on line 85
I believe there's some configuration that needs putting in place here, but I don't know how to create a class that implements ManagerRegistryInterface - if that is the right thing to do.
Any pointers?
Edit - here is my code for setting up Doctrine
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Setup;
class Bootstrap {
//...some other methods, including getCredentials() which returns DB credentials for Doctrine
public function getEntityManager($env){
$isDevMode = $env == 'dev';
$paths = [ROOT_DIR . '/src'];
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false);
$dbParams = $this->getCredentials($env);
$em = EntityManager::create($dbParams, $config);
return $em;
}
}
Expanding on the answer by xabbuh.
I was able to implement
EntityType
in theFormBuilder
without too much extra work. However it does not work with the annotations in order to useConstraints
directly inside the entity, which would require a lot more work.You can easily facilitate the
ManagerRegistry
requirement of the Doctrine ORM Forms Extension, by extending the existingAbstractManagerRegistry
and making your own container property within the customManagerRegistry
.Then it's just a matter of registering the Form extension just like any other extension (
ValidatorExtension
,HttpFoundationExtension
, etc).The ManagerRegistry
Configure the Forms to use the extension
Create the Form
The easiest way to solve your issue is by registering the DoctrineOrmExtension from the Doctrine bridge which makes sure that the entity type is registered with the needed dependencies.
So basically, the process of bootstrapping the Form component would look like this:
Believe me, you're asking for trouble!
EntityType::class
works when it is seamsly integrated to "Symfony" framework (there's magic under the hoods - via DoctrineBundle). Otherwise, you need to write a lot of code for it to work properly.Not worth the effort!
It's a lot easier if you to create an entity repository and inject it in form constructor, then use in a
ChoiceType::class
field. Somethink like this:And somewhere in application:
That's the point!