0

I'm trying to clear my form after the form submit but unfortunately, it doesn't work

Here's my controller.

#[Route('/', name: 'homepage', methods: ['GET', 'POST'])]
public function index(Request $request, FooRepo $fooRepo): Response
{
    $foo = new Foo();
    $form = $this->createForm(FooType::class, $foo);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $fooRepo->save($foo, true);

        if (TurboBundle::STREAM_FORMAT === $request->getPreferredFormat()) {
        // If the request comes from Turbo, set the content type as text/vnd.turbo-stream.html and only send the HTML to update
            $request->setRequestFormat(TurboBundle::STREAM_FORMAT);
            return $this->render('shared/success.html.twig', ['status' => "Success!"]);
        }

        return $this->redirect($this->generateUrl("homepage"));
    }

    return $this->render('home/index.html.twig', [
    controller_name' => 'HomeController',
    'form' => $form
    ]);
}

So far, I tried the unset and redirect from this How can I clear form values after successful form submission but no success.

Here's my sample UI:

{{ form_start(form) }}
<input type="text" name=" {{ field_name(form.name) }} " placeholder="enter name"/>

<button type="submit">Save</button>
{{ form_end(form) }}

I'm using turbo that's why there's no page reload when I hit submit. There's no issue in saving but when the ajax call finished, the field doesn't clear after the submission. Any help?

Joker Bench
  • 228
  • 1
  • 6
  • 14

0 Answers0