Skip to content
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

Adds usage in a mvc application to documentation #64

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
# Usage in a laminas-mvc Application

The following example shows _one_ potential use case of laminas-form within
a laminas-mvc based application. The example uses a module, a controller and the
laminas-form element manager.

The example is based on the [tutorial application](https://docs.laminas.dev/tutorials/getting-started/overview/)
which builds an album inventory system.

Before starting, make sure laminas-form is installed and configured.
froschdesign marked this conversation as resolved.
Show resolved Hide resolved

## Create Form

Create a form as separate class, e.g. `module/Album/src/Form/AlbumForm.php`:

```php
namespace Album\Form;

use Laminas\Filter\StringTrim;
use Laminas\Filter\StripTags;
use Laminas\Form\Element\Text;
use Laminas\Form\Form;
use Laminas\InputFilter\InputFilterProviderInterface;
use Laminas\Validator\StringLength;

class AlbumForm extends Form implements InputFilterProviderInterface
{
public function init() : void
{
// Title
$this->add([
'name' => 'title',
'type' => Text:class,
'options' => [
'label' => 'Title',
],
]);

// …
}

public function getInputFilterSpecification() : array
{
return [
// Title
[
froschdesign marked this conversation as resolved.
Show resolved Hide resolved
'name' => 'title',
'filters' => [
['name' => StripTags::class],
['name' => StringTrim::class],
],
'validators' => [
[
'name' => StringLength::class,
'options' => [
'min' => 1,
'max' => 100,
],
],
],
],
// …
];
}
}
```

## Using Form

### Create Controller

Using the form in a controller, e.g.
`module/Album/Controller/AlbumController.php`:

```php
namespace Album\Controller;

use Album\Form\AlbumForm;
use Laminas\Form\FormInterface;
use Laminas\Mvc\Controller\AbstractActionController;

class AlbumController extends AbstractActionController
{
/** @var FormInterface */
private $form;

public function __construct(AlbumForm $form)
{
$this->form = $form;
}

public function addAction()
{
// Set action attribute
$this->form->setAttribute(
'action',
$this->url()->fromRoute('album', ['action' => 'add'])
);

$variables = ['form' => $this->form];

if (! $this->getRequest()->isPost()) {
return $variables;
}

// Validation
$this->form->setData($this->getRequest()->getPost());
if (! $this->form->isValid()) {
return $variables;
froschdesign marked this conversation as resolved.
Show resolved Hide resolved
}

// …

return $this->redirect()->toRoute('album', ['action' => 'add']);
froschdesign marked this conversation as resolved.
Show resolved Hide resolved
}
}
```

### Create Factory for Controller

Fetch the `AlbumForm` from the form element manager in a factory,
e.g. `src/Album/Controller/AlbumControllerFactory.php`:

```php
namespace Album\Controller;

use Album\Form\AlbumForm;
use Laminas\ServiceManager\PluginManagerInterface;
use Psr\Container\ContainerInterface;

class AlbumControllerFactory
{
public function __invoke(ContainerInterface $container) : AlbumController
{
/** @var PluginManagerInterface $formElementManager */
$formElementManager = $container->get('FormElementManager');
/** @var AlbumForm */
$form = $formElementManager->get(AlbumForm::class);

return new AlbumController($form);
}
}
```

> ### Instantiating the Form
>
> The form element manager is used instead of directly instantiating the form to
> ensure to get the input filter manager injected. This allows usage of any
> input filter registered with the input filter managers which includes custom
> filters and validators.
> Custom form elements can also be used in this way.
>
> Additionally the form element manager calls the `init` method _after_
> instantiating the form, ensuring all dependencies are fully injected
> first.

## Register Form and Controller

If no separate factory is required for the form, then the form element manager
will instantiating the form class. Otherwise the form must be registered.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise the form must be registered.

A link must be added here when we have the description for the form element manager. (See #36)


To register the controller for the application, extend the configuration of the
module.
Add the following lines to the module configuration file, e.g.
`module/Album/config/module.config.php`:

<pre class="language-php" data-line="6-7"><code>
namespace Album;

return [
'controllers' => [
'factories' => [
// Add this line
Controller\AlbumController::class => Controller\AlbumControllerFactory::class,
],
],
// …
];
</code></pre>

## Create View Script

Create a view script in the module, e.g.
`module/Album/view/album/album/add.phtml`:

```php
<?php
/**
* @var Laminas\View\Renderer\PhpRenderer|Laminas\Form\View\HelperTrait $this
* @var Laminas\Form\Form $form
*/
$this->headTitle('Add new album');
?>

<h1>Add new album</h1>

<?= $this->form($form) ?>
```
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ nav:
- formFileSessionProgress: helper/form-file-session-progress.md
- formFileUploadProgress: helper/form-file-upload-progress.md
- Advanced: advanced.md
- "Application Integration":
- "Usage in a laminas-mvc application": application-integration/usage-in-a-laminas-mvc-application.md
site_name: laminas-form
site_description: "Validate and display simple and complex forms, casting forms to business objects and vice versa."
repo_url: 'https://github.com/laminas/laminas-form'
Expand Down