John Davidson

php - Form resend datas after refresh on Symfony 6

0 comments
Message:


Im beginner in Symfony :)


Ive created a website with Symfony 6.And i have a contact form page.
This form work great but, after submit, if i reload the page, this form submit again.


Here, the ContactController


#[Route('/contact', name: 'contact')]
public function contact(ManagerRegistry $doctrine, Request $request, MailerInterface $mailer): Response
{

$form = $this->createForm(ContactType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$email = (new Email())
->to("[email protected]")
->from($form->get("email")->getData())
->subject($form->get("title")->getData())
->text('Sending emails is fun again!')
->html('<p>See Twig integration for better HTML integration!</p>');

try {
$mailer->send($email);
$resultMail = "Le mail est envoyé ! Pensez à consulter vos spams.";
$this->addFlash(
"success",
"Votre message est bien envoyé."
);
} catch (TransportExceptionInterface $e) {
// some error prevented the email sending; display an
// error message or try to resend the message
$resultMail = "Il y a eu une erreur lors de l'envoi de l'email.";
$error = new Contact;
$error->setEmail($form->get("email")->getData());
$error->setTitle($form->get("title")->getData());
$error->setMessage($form->get("message")->getData());
$error->setDate(new DateTime());
$error->setError($e);

$em = $doctrine->getManager();
$em->persist($error);
$em->flush();
$this->addFlash(
"error",
"Une erreur s'est produite lors de l'envoi de votre message."
);
}
}

return $this->render('contact/contact.html.twig', [
"pageTitle" => "Contact",
"ssTitle" => "Vous avez besoin de mes services ?",
"contact_form" => $form->createView()
]);
}

How to fix it? And This is possible to redirect to contact anchor after submit ? (/contact#form) ?


Regards



My Answer:
php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Form\ContactType;

class ContactController extends AbstractController
{
/**
* @Route("/contact", name="contact")
*/
public function contact(Request $request): Response
{
$form = $this->createForm(ContactType::class);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
// Process form data here
// Redirect to a success page or do something else
}

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


To prevent the form from being submitted again when the page is refreshed, you can use the Post/Redirect/Get (PRG) pattern. After processing the form data, you can redirect the user to another page using the `redirectToRoute` method. This way, if the user refreshes the page, the form data will not be resubmitted.

Here's an example of how you can modify the controller to implement the PRG pattern:

php
if ($form->isSubmitted() && $form->isValid()) {
// Process form data here
// Redirect to a success page
return $this->redirectToRoute('success_page');
}


In this example, `success_page` is the name of the route to the success page. You can define this route in your routes configuration file (`config/routes.yaml`) and create a corresponding controller action to handle the success page.

By using the PRG pattern, you can prevent the form from being resubmitted when the page is refreshed.

Rate this post

3 of 5 based on 8633 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us