Laminas Forms

DoctrineModule and DoctrineORMModule provide an integration with laminas-form.

Creating Forms using Entity Annotations

With laminas-form, forms can be created using PHP8 attributes or DocBlock annotations. DoctrineORMModule extends this feature to support Doctrine-specific form elements (see next section).

First, create a form builder instance. By default, this uses the AnnotationBuilder from laminas-form, which uses DocBlock annotations. Alternatively, you can provide an AttributeBuilder to use PHP8-style attributes.

1// using PhpDoc annotations $entityManager = $container->get(\Doctrine\ORM\EntityManager::class); $builder = new \DoctrineORMModule\Form\Annotation\EntityBasedFormBuilder($entityManager); // alternatively, to use PHP8 attributes $entityManager = $container->get(\Doctrine\ORM\EntityManager::class); $attributeBuilder = new \Laminas\Form\Annotation\AttributeBuilder(); $builder = new \DoctrineORMModule\Form\Annotation\EntityBasedFormBuilder($entityManager, $attributeBuilder);
2
3
4
5
6
7
8

Given an entity instance, the form builder can either create a form specification or directly a form instance:

1$entity = new User(); // get form specification only $formSpec = $builder->getFormSpecification($entity); // or directly get form $form= $builder->createForm($entity);
2
3
4
5
6
7

Extension points for customizing the form builder are the event manager and the form factory, which can be accessed as follows:

1// if you need access to the event manager $myListener = new MyListener(); $myListener->attach($builder->getBuilder()->getEventManager()); // if you need access to the form factory $formElementManager = $container->get(\Laminas\Form\FormElementManager::class) $builder->getBuilder()->getFormFactory()->setFormElementManager($formElementManager);
2
3
4
5
6
7

Doctrine-specific Form Elements

DoctrineModule provides three Doctrine-specific form elements:

  • DoctrineModule\Form\Element\ObjectSelect
  • DoctrineModule\Form\Element\ObjectRadio
  • DoctrineModule\Form\Element\ObjectMultiCheckbox

Please read the DoctrineModule documentation on form elements for further information.

Doctrine-specific Validators

DoctrineModule provides three Doctrine-specific validators:

  • DoctrineModule\Validator\ObjectExists
  • DoctrineModule\Validator\NoObjectExists
  • DoctrineModule\Validator\UniqueObject

Please read the DoctrineModule documentation on validators for further information.