|
| 1 | +Laminas Forms |
| 2 | +============= |
| 3 | + |
| 4 | +DoctrineModule and DoctrineORMModule provide an integration with `laminas-form <https://docs.laminas.dev/laminas-form/>`_. |
| 5 | + |
| 6 | +Creating Forms using Entity Annotations |
| 7 | +--------------------------------------- |
| 8 | + |
| 9 | +With laminas-form, forms can be created using `PHP8 attributes or DocBlock annotations <https://docs.laminas.dev/laminas-form/v3/form-creation/attributes-or-annotations/>`_. |
| 10 | +DoctrineORMModule extends this feature to support Doctrine-specific form elements (see next section). |
| 11 | + |
| 12 | +First, create a form builder instance. By default, this uses the ``AnnotationBuilder`` from laminas-form, |
| 13 | +which uses DocBlock annotations. Alternatively, you can provide an ``AttributeBuilder`` to use PHP8-style |
| 14 | +attributes. |
| 15 | + |
| 16 | +.. code:: php |
| 17 | +
|
| 18 | + // using PhpDoc annotations |
| 19 | + $entityManager = $container->get(\Doctrine\ORM\EntityManager::class); |
| 20 | + $builder = new \DoctrineORMModule\Form\Annotation\EntityBasedFormBuilder($entityManager); |
| 21 | +
|
| 22 | + // alternatively, to use PHP8 attributes |
| 23 | + $entityManager = $container->get(\Doctrine\ORM\EntityManager::class); |
| 24 | + $attributeBuilder = new \Laminas\Form\Annotation\AttributeBuilder(); |
| 25 | + $builder = new \DoctrineORMModule\Form\Annotation\EntityBasedFormBuilder($entityManager, $attributeBuilder); |
| 26 | +
|
| 27 | +Given an entity instance, the form builder can either create a form specification or directly a form instance: |
| 28 | + |
| 29 | +.. code:: php |
| 30 | +
|
| 31 | + $entity = new User(); |
| 32 | +
|
| 33 | + // get form specification only |
| 34 | + $formSpec = $builder->getFormSpecification($entity); |
| 35 | +
|
| 36 | + // or directly get form |
| 37 | + $form= $builder->createForm($entity); |
| 38 | +
|
| 39 | +Extension points for customizing the form builder are the event manager and the form factory, which can |
| 40 | +be accessed as follows: |
| 41 | + |
| 42 | +.. code:: php |
| 43 | +
|
| 44 | + // if you need access to the event manager |
| 45 | + $myListener = new MyListener(); |
| 46 | + $myListener->attach($builder->getBuilder()->getEventManager()); |
| 47 | +
|
| 48 | + // if you need access to the form factory |
| 49 | + $formElementManager = $container->get(\Laminas\Form\FormElementManager::class) |
| 50 | + $builder->getBuilder()->getFormFactory()->setFormElementManager($formElementManager); |
| 51 | +
|
| 52 | +Doctrine-specific Form Elements |
| 53 | +------------------------------- |
| 54 | + |
| 55 | +DoctrineModule provides three Doctrine-specific form elements: |
| 56 | + |
| 57 | +- ``DoctrineModule\Form\Element\ObjectSelect`` |
| 58 | +- ``DoctrineModule\Form\Element\ObjectRadio`` |
| 59 | +- ``DoctrineModule\Form\Element\ObjectMultiCheckbox`` |
| 60 | + |
| 61 | +Please read the `DoctrineModule documentation on form elements <https://www.doctrine-project.org/projects/doctrine-module/en/current/form-element.html>`_ |
| 62 | +for further information. |
| 63 | + |
| 64 | +Doctrine-specific Validators |
| 65 | +---------------------------- |
| 66 | + |
| 67 | +DoctrineModule provides three Doctrine-specific validators: |
| 68 | + |
| 69 | +- ``DoctrineModule\Validator\ObjectExists`` |
| 70 | +- ``DoctrineModule\Validator\NoObjectExists`` |
| 71 | +- ``DoctrineModule\Validator\UniqueObject`` |
| 72 | + |
| 73 | +Please read the `DoctrineModule documentation on validators <https://www.doctrine-project.org/projects/doctrine-module/en/current/validator.html>`_ |
| 74 | +for further information. |
0 commit comments