0

I want to clean value fields of a Symfony form after submit but I don't know how to do it.

Form file

<?php

namespace App\Form;

use App\Entity\Comment;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class CommentType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('text',TextareaType::class)
            ->add('visible',HiddenType::class)
            ->add('user',HiddenType::class)
            ->add('recipe',HiddenType::class)
            ->add('save', SubmitType::class, [
                'label' => "Comentar",
                'attr' => ['class' => 'save'],
            ]);
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Comment::class,
        ]);
    }
}

Controller file

/**
 * @Route("/receta/{title}", name="recipe_show", methods={"GET"})
 * @Route("/receta/{title}", name="recipe_show")
 */
public function show(Recipe $recipe,RecipeRepository $recipeRepository,CommentRepository $commentRepository, Request $request): Response
{ 
    $comment = new Comment();

    $comment_form = $this->createForm(CommentType::class, $comment);
    $comment_form->handleRequest($request);

    if ($comment_form->isSubmitted() && $comment_form->isValid()) {
        $comment->setVisible(0);
        $user = $this->getUser();
        $comment->setUser($user);
        $comment->setRecipe($recipe);
        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($comment);
        $entityManager->flush();  

        $this->addFlash('success', '¡Comentario registrado con exito! Tu comentario estará visible una vez que el administrador lo revise.');
        
 
    }

    $comments = $commentRepository->findCommentsByRecipe($recipe);
    

    return $this->render('recipe/show/show.html.twig', [
        'recipe' => $recipe,
        'comment_form' => $comment_form->createView(),
        'comments' => $comments
    ]);
}

Specifically I need to clean text field after submit but I don't know how to do it. I need to show an addFlash after submit too.It can't miss in the process. How can I solve it?

Cristina
  • 1
  • 1
  • 5
  • That is why the docs show a redirect after submitting and successfully processing a form. The same docs also show using the flash message capability. – Cerad May 04 '21 at 17:56
  • Does this answer your question? [How can I clear form values after successful form submission](https://stackoverflow.com/questions/24229839/how-can-i-clear-form-values-after-successful-form-submission) – FTW May 04 '21 at 20:56
  • @Cerad can you give me the URL docs where it is. I don't see an example of both things together – Cristina May 05 '21 at 00:18
  • @FTW I tried it after but it don't works for me. I use Symfony 4.4 – Cristina May 05 '21 at 00:19
  • It was solved.Thanks Cerad! – Cristina May 05 '21 at 10:11

1 Answers1

0

As @Cerad suggests, you should try to read the docs more carefully. Symfony docs are very well maintained and easy to google:

https://symfony.com/doc/current/forms.html#processing-forms

If your controller extends Symfony's AbstractController, you can use the shortcut:

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

        // ... your form submission logic here

        $this->addFlash('success', '¡Comentario registrado con exito! Tu comentario estará visible una vez que el administrador lo revise.');
        
        // Add this line, it will redirect to the same page, without the post data:
        return $this->redirectToRoute('recipe_show', ['title' => 'sometitle']);
    }
  • I see that but I thought that when I redirect the addFlash will be miss. Thanks youy and thanks Cerad. – Cristina May 05 '21 at 10:10