0

I don't understand where should I put my form code in my Symfony 4 project. As I understand you should not have business logic in controller but you need to extend Controller to use $this->createForm.

class HomeController extends Controller
{
    public function index(Request $request)
    {
        $article = new Article();
        $form = $this->createForm(ArticleType::class, $article);
        $form->handleRequest($request);

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

           $entityManager = $this->getDoctrine()->getManager();
           $entityManager->persist($article);

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

Thanks

2 Answers 2

1

There is nothing wrong in using forms in Controller in my opinion. However if you want to keep your controllers slim you can move your business logic to services. For example:

App\Controllers\HomeController:

class HomeController extends Controller
{
    // Inject ArticleService
    public function index(Request $request, ArticleServiceInterface $articleService)
    {
        $article = new Article();
        $form = $this->createForm(ArticleType::class, $article);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {

            $articleService->create($form->getData());

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

App\Services\ArticleService:

class ArticleService implements ArticleServiceInterface
{
    private $entityManager;

    public function __construct(EntityManagerInterface $entityManager) {
        $this->entityManager = $entityManager;
    }

    public function create(Article $article)
    {
        // you may do some extra stuff here
        $this->entityManager->persist($article);
        $this->entityManager->flush();
    }
}

This is a complete overkill in this particular example, but gives an idea how it may be.

More info in Symfony docs.

Sign up to request clarification or add additional context in comments.

Comments

0

In one medium-big proyect (or to be more orderly) you must put in src/Form/Type/ folder (you must create it). In your example:

<?php
// src/Form/Type/ArticleType.php
namespace App\Form\Type;

use App\Entity\Article;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

//you must use the classes you use in the form
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;

class ArticleType extends AbstractType
{
   public function buildForm(FormBuilderInterface $builder, array $options)
   {
    //add your form code here
      $builder
         ->add()
         ->add ()
       ;
   }

   public function configureOptions(OptionsResolver $resolver)
   {
      $resolver->setDefaults(array(
      //write here your options
      ));
   }

   public function getBlockPrefix()
   {
      return null;
   }

}

2 Comments

Since OP is already calling this in controller: $form = $this->createForm(ArticleType::class, $article);, then I guess he's already defined ArticleType class.
Sure, but he asked where to put his form code and the Symfony's structure for large proyects is clear about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.