hostnet/form-handler-bundle

A small introduction


Yannick de Lange


                        function someAction(Request $request)
                        {
                            $form = $this->createForm(TaskType::class);
                            $form->handleRequest($request);

                            if ($form->isSubmitted() && $form->isValid()) {
                                $task = $form->getData();

                                $em = $this->getDoctrine()->getManager();
                                $em->persist($task);
                                $em->flush();

                                return $this->redirectToRoute('task_success');
                            }

                            return $this->render('default/new.html.twig', [
                                'form' => $form->createView(),
                            ]);
                        }
                    

                        function someAction(Request $request)
                        {
                            $form = $this->createForm(TaskType::class);
                            $form->handleRequest($request);

                            if ($form->isSubmitted() && $form->isValid()) {
                                $task = $form->getData();

                                $em = $this->getDoctrine()->getManager();
                                $em->persist($task);
                                $em->flush();

                                $this->get('event_dispatcher')->dispatch(
                                    TaskEvents::MANUAL_CREATE,
                                    new TaskEvent($task)
                                );

                                return $this->redirectToRoute('task_success');
                            }

                            return $this->render('default/new.html.twig', [
                                'form' => $form->createView(),
                            ]);
                        }
                    

                        function someAction(Request $request)
                        {
                            $form = $this->createForm(TaskType::class);
                            $form->handleRequest($request);

                            if ($form->isSubmitted() && $form->isValid()) {
                                $task = $form->getData();

                                $em = $this->getDoctrine()->getManager();
                                $em->persist($task);
                                $em->flush();

                                $this->get('event_dispatcher')->dispatch(
                                    TaskEvents::MANUAL_CREATE,
                                    new TaskEvent($task)
                                );

                                $message = \Swift_Message::newInstance()
                                    ->setSubject('Task created')
                                    ->setFrom($this->fromEmail)
                                    ->setTo($this->toEmail)
                                    ->setBody($this->renderView(
                                        'AcmeBundle:Task:create.txt.twig',
                                        ['task' => $task]
                                    ))
                                ;
                                $this->get('mailer')->send($message);

                                return $this->redirectToRoute('task_success');
                            }

                            return $this->render('default/new.html.twig', [
                                'form' => $form->createView(),
                            ]);
                        }
                    

"thin controllers and fat models"

"... should hold just the thin layer of glue-code needed to coordinate the different parts of the application."

- Symfony Best Practices

Forms make it difficult to have thin controllers

Introducing hostnet/form-handler-bundle

Reducing boilerplate code

Thinner controllers

Typehinting data object


                        final class TaskHandlerType implements HandlerTypeInterface
                        {
                            // ...

                            public function configure(HandlerConfigInterface $config)
                            {
                                $config->setType(TaskType::class);
                                $config->onSuccess([$this, 'onSuccess']);
                            }

                            public function onSuccess(Task $task)
                            {
                                $this->em->persist($task);
                                $this->em->flush();

                                $this->dispatcher->dispatch(
                                    TaskEvents::MANUAL_CREATE,
                                    new TaskEvent($task)
                                );

                                $message = \Swift_Message::newInstance()
                                    ->setSubject('Task created')
                                    ->setFrom($this->fromEmail)
                                    ->setTo($this->toEmail)
                                    ->setBody($this->twig->render(
                                        'AcmeBundle:Task:create.txt.twig',
                                        ['task' => $task]
                                    ))
                                ;
                                $this->mailer->send($message);

                                return new RedirectResponse($this->router->generate('task_success'));
                            });
                        }
                    

                        function someAction(Request $request)
                        {
                            $handler = $this->get('hostnet.form_handler.factory')
                                ->create(TaskHandlerType::class);

                            if (($response = $handler->handle($request)) instanceof Response) {
                                return $response;
                            }

                            return $this->render('default/new.html.twig', [
                                'form' => $handler->getForm()->createView()
                            ]);
                        }
                    

https://github.com/hostnet/form-handler-bundle

#hostnet-form-handlers @ symfony-devs