Skip to content

Using Named Forms

Yannick de Lange edited this page Apr 6, 2017 · 6 revisions

In some cases you might want to create a named form. The form handlers support this by also configuring a name. This is useful if you have the same form twice on a page and want to be able to differentiate them.

You can do this as follows:

<?php
namespace App\FormHandler;

use App\FormType\EditUserType;
use Hostnet\Component\FormHandler\HandlerConfigInterface;
use Hostnet\Component\FormHandler\HandlerTypeInterface;

final class EditUserFormHandler implements HandlerTypeInterface
{
    public function __construct(/* ... */) {/* ... */}

    public function configure(HandlerConfigInterface $config)
    {
        $config->setType(EditUserType::class);

        // to allow the same form on the same page multiple times
        $config->setName('my_custom_form_name');

        $config->onSuccess(function (Account $user) {
            // ...
        });
    }
}

This will internally call the FormFactoryInterface::createNamed() method with the name instead of the regular FormFactoryInterface::create(). This way Symfony can better tell which form was submitted.

Clone this wiki locally