BusForm #38

Closed
garak opened this Issue Nov 30, 2016 · 3 comments

Comments

Projects
None yet
2 participants
@garak
Contributor

garak commented Nov 30, 2016

This is an idea I had just yesterday, and I'd like to hear some opinions about it.
Since I'm using every time forms with commands associated, I created a base BusType, with the bus injected. The idea is that my forms are extending such BusType (instead of classic AbstractType), so the handling of the form can be automatic. Of course, this means that all forms need to be declared as service, but this is also saving me from calling every time the handle method of command_bus service in controllers. As a bonus, the form is now able to catch a DomainException and transform it in a form error.
Here is the code:

<?php

namespace AppBundle\Form;

use SimpleBus\Message\Bus\Middleware\MessageBusSupportingMiddleware as Bus;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

abstract class BusType extends AbstractType
{
    /**
     * @var Bus
     */
    private $bus;

    /**
     * @param Bus $bus
     */
    public function __construct(Bus $bus)
    {
        $this->bus = $bus;
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use ($options) {
            $this->handle($event, $options['data']);
        });
    }

    /**
     * @param FormEvent $event
     * @param mixed     $command
     */
    public function handle(FormEvent $event, $command)
    {
        if (!$event->getForm()->isValid()) {
            return;
        }
        try {
            $this->bus->handle($command);
        } catch (\DomainException $exception) {
            $event->getForm()->addError(new FormError($exception->getMessage()));
        }
    }
}

Here is an example of form:

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use MyDomain\Command\DoSomethingCommand;

class FooType extends BusType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder
            ->add('bar')
            ->add('baz')
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => DoSomethingCommand::class,
        ]);
    }
}
@cmodijk

This comment has been minimized.

Show comment
Hide comment
@cmodijk

cmodijk Dec 1, 2016

Member

@garak Tnx for this idea. At the moment we are triggering the command from within our controllers. I think is is also the best praktisch in this case although i know it is repetitive to get the command from the form and hand it to the bus i think that symfony form types should never have a side effect when data is submitted to it. This could be a solution for your own project but i don't think this is something that should be part of the SimpleBus core packages.

What you could do is create a new symfony bundle and release that?

Member

cmodijk commented Dec 1, 2016

@garak Tnx for this idea. At the moment we are triggering the command from within our controllers. I think is is also the best praktisch in this case although i know it is repetitive to get the command from the form and hand it to the bus i think that symfony form types should never have a side effect when data is submitted to it. This could be a solution for your own project but i don't think this is something that should be part of the SimpleBus core packages.

What you could do is create a new symfony bundle and release that?

@garak

This comment has been minimized.

Show comment
Hide comment
@garak

garak Dec 5, 2016

Contributor

Thanks for your suggestion. I created a package named PUGXBusFormBundle.

Contributor

garak commented Dec 5, 2016

Thanks for your suggestion. I created a package named PUGXBusFormBundle.

@garak garak closed this Dec 5, 2016

@cmodijk

This comment has been minimized.

Show comment
Hide comment
@cmodijk

cmodijk Dec 5, 2016

Member

@garak Tnx, we are currently working on the new docs site for SimpleBus i created a issue to note al the other packages and integrations like yours.

Member

cmodijk commented Dec 5, 2016

@garak Tnx, we are currently working on the new docs site for SimpleBus i created a issue to note al the other packages and integrations like yours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment