Skip to content

Configuring Form Options

Iltar van der Berg edited this page Apr 6, 2017 · 1 revision

When configuring your handler type, you can also pass form options. Those options can make it easier to re-use form types within your application. The setOptions() method can be called with an array of options, which are passed to the form once created. However, you can also pass a callable, which allows you to create options based on your data. The callable has one argument, which is always the data object as passed along to the handle method in your controller. The setOptions() method is called before form submission, which means that you receive the data as passed along from your controller.

<?php
namespace App\FormHandler;

use App\Entity\Account;
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);

        // a simple array
        $config->setOptions(['some_custom_option' => true]);

        // or a callable
        $config->setOptions(function (Account $account) {
            return ['some_custom_option' => $account->isActive()];
        });

        $config->onSuccess(function (Account $account) {
            // ...
        });
    }
}
Clone this wiki locally