-
-
Notifications
You must be signed in to change notification settings - Fork 58
Adds description for the form element manager to the documentation #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
froschdesign
wants to merge
6
commits into
laminas:3.7.x
Choose a base branch
from
froschdesign:hotfix/docs/form-element-manager
base: 3.7.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b570196
Adds description for the form element manager to the documentation
froschdesign c18c91c
Updates texts in terms of wording and spelling
froschdesign f23b379
Extends form element manager description
froschdesign ca0e9bc
Adds description for usage in a laminas-mvc based application
froschdesign 21561dc
Adds important links to homepage
froschdesign 7dc3da6
Updates texts in terms of wording and spelling
froschdesign File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| ## Installation | ||
|
|
||
| ### Using Composer | ||
|
|
||
| ```bash | ||
| $ composer require laminas/laminas-form | ||
| ``` | ||
|
|
||
| ## Learn | ||
|
|
||
| <ul class="list-group list-group-flush"> | ||
| <li class="list-group-item"> | ||
| <a href="/laminas-form/v3/quick-start">Quick start</a> | ||
| </li> | ||
| <li class="list-group-item"> | ||
| <a href="/laminas-form/v3/application-integration/usage-in-a-laminas-mvc-application">Usage in a laminas-mvc application</a> | ||
| </li> | ||
| <li class="list-group-item"> | ||
| <a href="/laminas-form/v3/form-element-manager">The basics of the form element manager</a> | ||
| </li> | ||
| </ul> |
283 changes: 283 additions & 0 deletions
283
docs/book/v3/application-integration/form-element-manager-mvc.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,283 @@ | ||
| # Usage of Form Element Manager in a laminas-mvc Application | ||
|
|
||
| INFO: | ||
| The following examples show the usage of the form element manager in a laminas-mvc based application. | ||
| All **the basics of the form element manager** [can be found in a separate section](../form-element-manager.md). | ||
|
|
||
| ## Using the Form Element Manager in a Controller | ||
|
|
||
| ### Create Controller | ||
|
|
||
| [Create a controller class](https://docs.laminas.dev/laminas-mvc/quick-start/#create-a-controller) and inject the form element manager via the constructor, e.g. `module/Album/Controller/AlbumController.php`: | ||
|
|
||
| ```php | ||
| namespace Album\Controller; | ||
|
|
||
| use Laminas\Form\FormElementManager; | ||
| use Laminas\Mvc\Controller\AbstractActionController; | ||
|
|
||
| final class AlbumController extends AbstractActionController | ||
| { | ||
| public function __construct( | ||
| public readonly FormElementManager $formElementManager | ||
| ) {} | ||
| } | ||
| ``` | ||
|
|
||
| ### Register Controller | ||
|
|
||
| In a laminas-mvc based application, the form element manager is registered in the application during the [installation of laminas-form](../installation.md#installation-for-mezzio-and-laminas-mvc-application). | ||
| This allows to fetch the form element manager from the application service container. | ||
froschdesign marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| With the [reflection factory of laminas-servicemanager](https://docs.laminas.dev/laminas-servicemanager/reflection-abstract-factory/), the form element manager can be automaticly injected into the controller. | ||
froschdesign marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| To [register the controller](https://docs.laminas.dev/laminas-mvc/quick-start/#create-a-route) for the application, extend the configuration of the module. | ||
froschdesign marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Add the following lines to the module configuration file, e.g. `module/Album/config/module.config.php`: | ||
|
|
||
| <pre class="language-php" data-line="8-9"><code> | ||
| namespace Album; | ||
|
|
||
| use Laminas\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory; | ||
|
|
||
| return [ | ||
| 'controllers' => [ | ||
| 'factories' => [ | ||
| // Add this line | ||
| Controller\AlbumController::class => ReflectionBasedAbstractFactory::class, | ||
| ], | ||
| ], | ||
| // … | ||
| ]; | ||
| </code></pre> | ||
|
|
||
| ## Fetch a Form without Registration | ||
|
|
||
| The following example creates a class as form: | ||
froschdesign marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```php | ||
| final class AlbumForm extends Laminas\Form\Form | ||
| { | ||
| public function init(): void | ||
| { | ||
| // … | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Extend the controller and fetch the form: | ||
|
|
||
| <pre class="language-php" data-line="3,7,17-18"><code> | ||
| namespace Album\Controller; | ||
|
|
||
| use Album\Form\AlbumForm; | ||
| use Laminas\Form\FormElementManager; | ||
| use Laminas\Mvc\Controller\AbstractActionController; | ||
|
|
||
| use function assert; | ||
|
|
||
| final class AlbumController extends AbstractActionController | ||
| { | ||
| public function __construct( | ||
| public readonly FormElementManager $formElementManager | ||
| ) {} | ||
|
|
||
| public function addAction() | ||
| { | ||
| $form = $this->formElementManager->get(AlbumForm::class); | ||
| assert($form instanceof AlbumForm); | ||
|
|
||
| // … | ||
| } | ||
| } | ||
| </code></pre> | ||
|
|
||
| ## Register and Fetch a Form With a Factory | ||
|
|
||
| If a form needs some preparation for creation then a factory can be used. | ||
|
|
||
| The following example creates a class as factory for the form: | ||
froschdesign marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```php | ||
| final class AlbumFormFactory | ||
| { | ||
| public function __invoke(Psr\Container\ContainerInterface): AlbumForm | ||
| { | ||
| $form = new AlbumForm(); | ||
| $form->setName('album'); | ||
|
|
||
| return $form; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Register the form on the form element manager via the configuration key `form_elements` in the module configuration, e.g. `module/Album/config/module.config.php`: | ||
|
|
||
| <pre class="language-php" data-line="6-7"><code> | ||
| namespace Album; | ||
|
|
||
| return [ | ||
| 'form_elements' => [ | ||
| 'factories' => [ | ||
| // Add this line | ||
| Form\AlbumForm::class => Form\AlbumFormFactory::class, | ||
| ], | ||
| ], | ||
| // … | ||
| ]; | ||
| </code></pre> | ||
|
|
||
| Get the form and the name in a controller: | ||
froschdesign marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```php | ||
| namespace Album\Controller; | ||
|
|
||
| use Album\Form\AlbumForm; | ||
| use Laminas\Form\FormElementManager; | ||
| use Laminas\Mvc\Controller\AbstractActionController; | ||
|
|
||
| use function assert; | ||
|
|
||
| final class AlbumController extends AbstractActionController | ||
| { | ||
| // … | ||
|
|
||
| public function addAction() | ||
| { | ||
| $form = $this->formElementManager->get(AlbumForm::class); | ||
| assert($form instanceof AlbumForm); | ||
|
|
||
| echo $form->getName(); // album | ||
|
|
||
| // … | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Now the custom factory will be used to instance the form. | ||
froschdesign marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Using a Custom Element without Registration | ||
|
|
||
| The form element manager [allows fetching custom elements without prior registration](../form-element-manager.md#usage-of-the-form-element-manager-in-a-form) with the manager. | ||
|
|
||
| The following example creates a custom element: | ||
|
|
||
| ```php | ||
| final class ExampleElement extends Laminas\Form\Element | ||
| { | ||
| // … | ||
| } | ||
| ``` | ||
|
|
||
| Create a form and add the custom element: | ||
|
|
||
| ```php | ||
| final class ExampleForm extends Laminas\Form\Form | ||
| { | ||
| public function init(): void | ||
| { | ||
| $this->add([ | ||
| 'type' => ExampleElement::class, | ||
| 'name' => 'example', | ||
| 'options' => [ | ||
| 'label' => 'Example element' | ||
| ], | ||
| ]); | ||
|
|
||
| // … | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Fetch the form and the element in a controller: | ||
|
|
||
| ```php | ||
| namespace Album\Controller; | ||
|
|
||
| use Album\Form\AlbumForm; | ||
| use Laminas\Form\FormElementManager; | ||
| use Laminas\Mvc\Controller\AbstractActionController; | ||
|
|
||
| use function assert; | ||
|
|
||
| final class AlbumController extends AbstractActionController | ||
| { | ||
| // … | ||
|
|
||
| public function addAction() | ||
| { | ||
| $form = $this->formElementManager->get(AlbumForm::class); | ||
| assert($form instanceof AlbumForm); | ||
|
|
||
| echo $form->get('example')->getLabel(); // Example element | ||
|
|
||
| // … | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Register and Using a Custom Element with a Factory | ||
froschdesign marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| If a custom element needs some preparation for creation then a factory can be used. | ||
|
|
||
| The following example creates a class as factory for the element of the previous example: | ||
froschdesign marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```php | ||
| final class ExampleElementFactory | ||
| { | ||
| public function __invoke(Psr\Container\ContainerInterface): ExampleElement | ||
| { | ||
| $element = new ExampleElement(); | ||
| $element->setOption('example_param', 'value'); | ||
|
|
||
| return $element; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Register the element on the form element manager via the configuration key `form_elements` in the module configuration, e.g. `module/Album/config/module.config.php`: | ||
|
|
||
| <pre class="language-php" data-line="6-7"><code> | ||
| namespace Album; | ||
|
|
||
| return [ | ||
| 'form_elements' => [ | ||
| 'factories' => [ | ||
| // Add this line | ||
| Form\ExampleElement::class => Form\ExampleElementFactory::class, | ||
| ], | ||
| ], | ||
| // … | ||
| ]; | ||
| </code></pre> | ||
|
|
||
| Now the custom factory will be used to instance the element. | ||
froschdesign marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Configuring the Form Element Manager | ||
|
|
||
| The [configuration of the form element manager follows the exact same pattern](https://docs.laminas.dev/laminas-servicemanager/configuring-the-service-manager/) as for a normal service manager of laminas-servicemanager. | ||
|
|
||
| In a laminas-mvc based application this means to use the application or module configuration, like `config/autload/global.php` or `module/Album/config/module.config.php`, and the configuration key `form_elements`: | ||
froschdesign marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```php | ||
| return [ | ||
| 'form_elements' => [ | ||
| 'factories' => [ | ||
| Album\Form\ExampleElement::class => Album\Form\ExampleElementFactory::class, | ||
| ], | ||
| 'aliases' => [ | ||
| 'example' => Album\Form\ExampleElement::class, | ||
| ], | ||
| 'abstract_factories' => [], | ||
| 'delegators' => [], | ||
| // … | ||
| ] | ||
| ]; | ||
| ``` | ||
|
|
||
| The factory `Laminas\Form\FormElementManagerFactory` uses the configuration, search for the configuration key `form_elements` and create the form element manager. | ||
froschdesign marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Learn More | ||
|
|
||
| - [The basics of the form element manager](../form-element-manager.md) | ||
| - [Creating custom elements](../advanced.md#creating-custom-elements) | ||
| - [Handling dependencies](../advanced.md#handling-dependencies) | ||
| - [Configuring the service manager](https://docs.laminas.dev/laminas-servicemanager/configuring-the-service-manager/) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.