src/Form/Type/ContactFormType.php line 108

Open in your IDE?
  1. <?php
  2. declare(strict_types 1);
  3. namespace App\Form\Type;
  4. use App\Domain\DTO\ContactDTO;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  7. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  8. use Symfony\Component\Form\Extension\Core\Type\FileType;
  9. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  10. use Symfony\Component\Form\Extension\Core\Type\TextType;
  11. use Symfony\Component\Form\FormBuilderInterface;
  12. use Symfony\Component\Form\FormInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\OptionsResolver\OptionsResolver;
  15. use Symfony\Component\Validator\Constraints\Email;
  16. use Symfony\Component\Validator\Constraints\NotBlank;
  17. class ContactFormType extends AbstractType
  18. {
  19.     public function buildForm(
  20.         FormBuilderInterface $builder,
  21.         array $options
  22.     ) {
  23.         $path Request::createFromGlobals();
  24.         $path $path->getPathInfo();
  25.         $builder
  26.             ->add('name'TextType::class, [
  27.                 'label' => false,
  28.                 'mapped' => false,
  29.                 'constraints' => [
  30.                     new NotBlank([
  31.                         'message' => 'Merci de remplir ce champ',
  32.                     ]),
  33.                 ]
  34.             ])
  35.             ->add('firstname'TextType::class, [
  36.                 'label' => false,
  37.                 'mapped' => false,
  38.                 'constraints' => [
  39.                     new NotBlank([
  40.                         'message' => 'Merci de remplir ce champ',
  41.                     ]),
  42.                 ],
  43.                 'required' => true
  44.             ])
  45.             ->add('mail'EmailType::class, [
  46.                 'label' => false,
  47.                 'mapped' => false,
  48.                 'constraints' => [
  49.                     new Email([
  50.                         'message' => 'Merci d\'entrer un mail valide',
  51.                     ]),
  52.                     new NotBlank([
  53.                         'message' => 'Merci de remplir ce champ',
  54.                     ]),
  55.                 ]
  56.             ])
  57.             ->add('phone'TextType::class, [
  58.                 'label' => false,
  59.                 'mapped' => false,
  60.                 'required' => true,
  61.                 'constraints' => [
  62.                     new NotBlank([
  63.                         'message' => 'Merci de remplir ce champ',
  64.                     ]),
  65.                 ]
  66.             ])
  67.             ->add('file'FileType::class, [
  68.                 'label' => false,
  69.                 'mapped' => false,
  70.                 'required' => false
  71.             ])
  72.             ->add('object'ChoiceType::class, [
  73.                 'choices'  => [
  74.                     'Je recherche un bien' => 'Je recherche un bien',
  75.                     'Je vends un bien' => 'Je vends un bien',
  76.                     'Je souhaite plus d’informations sur un projet' => 'Je souhaite plus d’informations sur un projet',
  77.                     'Autre' => 'Autre'
  78.                 ],
  79.                 'label' => false,
  80.                 'mapped' => false,
  81.                 'constraints' => [
  82.                     new NotBlank([
  83.                         'message' => 'Merci de remplir ce champ',
  84.                     ]),
  85.                 ],
  86.                 'data' => ($path === '/vendre') ? 'Je vends un bien' 'Je recherche un bien'
  87.             ])
  88.             ->add('message'TextareaType::class, [
  89.                 'label' => false,
  90.                 'mapped' => false,
  91.                 'constraints' => [
  92.                     new NotBlank([
  93.                         'message' => 'Merci de remplir ce champ',
  94.                     ]),
  95.                 ]
  96.             ]);
  97.     }
  98.     public function configureOptions(OptionsResolver $resolver)
  99.     {
  100.         $resolver->setDefaults([
  101.             'data_class' => ContactDTO::class,
  102.             'empty_data' => function (FormInterface $form) {
  103.                 return new ContactDTO(
  104.                     $form->get('name')->getData(),
  105.                     $form->get('firstname')->getData(),
  106.                     $form->get('mail')->getData(),
  107.                     $form->get('phone')->getData(),
  108.                     $form->get('file')->getData(),
  109.                     $form->get('object')->getData(),
  110.                     $form->get('message')->getData()
  111.                 );
  112.             }
  113.         ]);
  114.     }
  115. }