Skip to content

Commit 034c707

Browse files
authored
Merge pull request #700 from driehle/feature/form-builder-docs
Added docs for `EntityBasedFormBuilder`
2 parents c066ae7 + cfdd83d commit 034c707

File tree

6 files changed

+78
-27
lines changed

6 files changed

+78
-27
lines changed

LICENSE renamed to LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2006-2021 Doctrine Project
1+
Copyright (c) 2006-2022 Doctrine Project
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
44
documentation files (the "Software"), to deal in the Software without restriction, including without limitation

docs/en/developer-tools.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Laminas Developer Tools in DoctrineORMModule
2-
=========================================
2+
============================================
33

44
If you ever tried `Laminas Developer
55
Tools <https://github.com/laminas/laminas-developer-tools>`__ you will

docs/en/forms.rst

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.

docs/en/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ Next Steps
5959
configuration
6060
cache
6161
migrations
62+
forms
6263
miscellaneous

docs/en/miscellaneous.rst

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,6 @@ Miscellaneous
44
The items listed below are optional and intended to enhance
55
integration between Laminas and Doctrine ORM.
66

7-
ObjectExists Validator and NoObjectExists Validator
8-
----------------------------------------------------
9-
10-
ObjectExists and NoObjectExists are validators similar to
11-
`Laminas Validators <https://docs.laminas.dev/laminas-validator/>`_.
12-
You can pass a variety of options to determine validity.
13-
The most basic use case requires an entity manager, an entity, and
14-
a field. You also have the option of specifying a query\_builder Closure
15-
to use if you want to fine tune the results.
16-
17-
.. code:: php
18-
19-
<?php
20-
$validator = new \DoctrineModule\Validator\NoObjectExists([
21-
// object repository to lookup
22-
'object_repository' => $serviceLocator->get('doctrine.entitymanager.orm_default')
23-
->getRepository('Db\Entity\User'),
24-
25-
// fields to match
26-
'fields' => ['username'],
27-
]);
28-
29-
// following works also with simple values if the number of fields to be matched is 1
30-
echo $validator->isValid(['username' => 'test']) ? 'Valid' : 'Invalid. A duplicate was found.';
31-
327
Authentication Adapter
338
----------------------
349

docs/en/sidebar.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
configuration
88
cache
99
migrations
10+
forms
1011
miscellaneous

0 commit comments

Comments
 (0)