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?