src/Form/Type/ContactFormType.php line 100

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.                 'constraints' => [
  29.                     new NotBlank([
  30.                         'message' => 'Merci de remplir ce champ',
  31.                     ]),
  32.                 ]
  33.             ])
  34.             ->add('firstname'TextType::class, [
  35.                 'label' => false,
  36.                 'constraints' => [
  37.                     new NotBlank([
  38.                         'message' => 'Merci de remplir ce champ',
  39.                     ]),
  40.                 ],
  41.                 'required' => false
  42.             ])
  43.             ->add('mail'EmailType::class, [
  44.                 'label' => false,
  45.                 'constraints' => [
  46.                     new Email([
  47.                         'message' => 'Merci d\'entrer un mail valide',
  48.                     ]),
  49.                     new NotBlank([
  50.                         'message' => 'Merci de remplir ce champ',
  51.                     ]),
  52.                 ]
  53.             ])
  54.             ->add('phone'TextType::class, [
  55.                 'label' => false,
  56.                 'constraints' => [
  57.                     new NotBlank([
  58.                         'message' => 'Merci de remplir ce champ',
  59.                     ]),
  60.                 ]
  61.             ])
  62.             ->add('file'FileType::class, [
  63.                 'label' => false,
  64.                 'required' => false
  65.             ])
  66.             ->add('object'ChoiceType::class, [
  67.                 'choices'  => [
  68.                     'Je recherche un bien' => 'Je recherche un bien' ,
  69.                     'Je postule' => 'Je postule',
  70.                     'Je souhaite vendre un bien' => 'Je souhaite vendre un bien',
  71.                     'Autre' => 'Autre'
  72.                 ],
  73.                 'label' => false,
  74.                 'constraints' => [
  75.                     new NotBlank([
  76.                         'message' => 'Merci de remplir ce champ',
  77.                     ]),
  78.                 ],
  79.                 'data' => ($path === '/vendre') ? 'Je souhaite vendre un bien' 'Je recherche un bien'
  80.             ])
  81.             ->add('message'TextareaType::class, [
  82.                 'label' => false,
  83.                 'constraints' => [
  84.                     new NotBlank([
  85.                         'message' => 'Merci de remplir ce champ',
  86.                     ]),
  87.                 ]
  88.             ]);
  89.     }
  90.     public function configureOptions(OptionsResolver $resolver)
  91.     {
  92.         $resolver->setDefaults([
  93.             'data_class' => ContactDTO::class,
  94.             'empty_data' => function (FormInterface $form) {
  95.                 return new ContactDTO(
  96.                     $form->get('name')->getData(),
  97.                     $form->get('firstname')->getData(),
  98.                     $form->get('mail')->getData(),
  99.                     $form->get('phone')->getData(),
  100.                     $form->get('file')->getData(),
  101.                     $form->get('object')->getData(),
  102.                     $form->get('message')->getData()
  103.                 );
  104.             }
  105.         ]);
  106.     }
  107. }