提交后如何清理值字段symfony表单

how to clean a value field symfony form after submit

我想在提交后清除 Symfony 表单的值字段,但我不知道该怎么做。

表格文件

<?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,
        ]);
    }
}

控制器文件

/**
 * @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
    ]);
}

具体来说,我需要在提交后清理文本字段,但我不知道该怎么做。我需要在提交 too.It 之后显示一个 addFlash 不能错过这个过程。我该如何解决?

正如@Cerad 建议的那样,您应该尝试更仔细地阅读文档。 Symfony 文档维护得很好并且易于 google:

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

如果你的控制器扩展了 Symfony 的 AbstractController,你可以使用快捷方式:

   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']);
    }