src/Controller/Front/ContactController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Form\ContactType;
  4. use App\Model\ContactDTO;
  5. use App\Services\MailService;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class ContactController extends AbstractController
  11. {
  12.     /**
  13.      * @Route("/contact", name="front_contact")
  14.      */
  15.     public function contact()
  16.     {
  17.         return $this->render('front/contact.html.twig');
  18.     }
  19.     /**
  20.      * @Route("/contact/submit-form", name="front_contact_form")
  21.      */
  22.     public function formRenderer(Request $requestMailService $mailService)
  23.     {
  24.         $contactDTO = new ContactDTO();
  25.         $form $this->createForm(ContactType::class, $contactDTO)->handleRequest($request);
  26.         if ($form->isSubmitted()) {
  27.             $arrayErrors = [];
  28.             if (!$contactDTO->getFirstname()) {
  29.                 $arrayErrors['firstname'] = 'Veuillez renseigner votre prénom';
  30.             }
  31.             if (!$contactDTO->getLastname()) {
  32.                 $arrayErrors['lastname'] = 'Veuillez renseigner votre nom';
  33.             }
  34.             if (!$contactDTO->getEmail()) {
  35.                 $arrayErrors['email'] = 'Veuillez renseigner votre email';
  36.             }
  37.             if (!$contactDTO->getPhone()) {
  38.                 $arrayErrors['phone'] = 'Veuillez renseigner votre N° de téléphone';
  39.             }
  40.             if (!$contactDTO->getCompany()) {
  41.                 $arrayErrors['company'] = 'Veuillez renseigner votre société';
  42.             }
  43.             if (!$contactDTO->getMessage()) {
  44.                 $arrayErrors['message'] = 'Veuillez renseigner le message';
  45.             }
  46.             if (!empty($arrayErrors)) {
  47.                 return $this->json([
  48.                     'status' => 'error',
  49.                     'errors' => $arrayErrors
  50.                 ]);
  51.             }
  52.             $mailService->sendMail($contactDTO);
  53.             return $this->json([
  54.                 'status' => 'ok',
  55.                 'message' => $arrayErrors
  56.             ], Response::HTTP_OK);
  57.         }
  58.         return $this->render('front/inc/contact_form.html.twig', [
  59.             'form' => $form->createView()
  60.         ]);
  61.     }
  62. }