src/Form/Contact/ContactType.php line 27

Open in your IDE?
  1. <?php
  2. // src/Form/Type/TaskType.php
  3. namespace App\Form\Contact;
  4. use App\Entity\Contact\Contact;
  5. use App\Form\Contact\ContactLangType;
  6. use Symfony\Component\Form\AbstractType;
  7. use Eo\HoneypotBundle\Form\Type\HoneypotType;
  8. use Symfony\Component\Form\FormBuilderInterface;
  9. use Symfony\Component\Validator\Constraints\Type;
  10. use Symfony\Component\Validator\Constraints\Email;
  11. use Symfony\Component\Validator\Constraints\Regex;
  12. use Symfony\Component\Validator\Constraints\NotBlank;
  13. use Symfony\Component\OptionsResolver\OptionsResolver;
  14. use Symfony\Component\Form\Extension\Core\Type\TextType;
  15. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  16. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  17. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  18. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  19. use Symfony\Component\Form\FormError;
  20. use Symfony\Component\Form\FormEvent;
  21. use Symfony\Component\Form\FormEvents;
  22. use Symfony\Contracts\Translation\TranslatorInterface;
  23. class ContactType extends AbstractType
  24. {
  25.     private TranslatorInterface $translator;
  26.     public function __construct(TranslatorInterface $translator)
  27.     {
  28.         $this->translator $translator;
  29.     }
  30.     public function buildForm(FormBuilderInterface $builder, array $options): void
  31.     {
  32.         $builder
  33.         ->add('name',TextType::class, [ 
  34.             'constraints' => [
  35.                 new NotBlank(['message' => 'msg_required_name'])                       
  36.             ],
  37.                 'attr' => [
  38.                         'placeholder' => 'Name*',
  39.                         'class' => "data-contact"
  40.                     ]
  41.             ])  
  42.             ->add('entreprise',TextType::class, [ 
  43.                 'constraints' => [
  44.                     new NotBlank(['message' => 'msg_required_company'])                       
  45.                 ],
  46.                     'attr' => [
  47.                             'placeholder' => 'Entreprise*',
  48.                             'class' => "data-contact"
  49.                         ]
  50.                 ])            
  51.            
  52.             ->add('phone'NumberType::class, [
  53.                 'constraints' => [
  54.                     new Type(['type' => 'numeric''message' =>  'form.tel.not_valid']),
  55.                 ],
  56.                 'attr' => [
  57.                     'placeholder' => 'Phone(facultatif)',
  58.                 ]
  59.             ])
  60.             
  61.             ->add('email',TextType::class, [ 
  62.                 'constraints' => [
  63.                     new NotBlank(['message' => 'msg_required_email']),
  64.                     new Email(['message' => 'form.email.not_valid'])
  65.                 ],
  66.                 'attr' =>[
  67.                         'placeholder' => 'Email*',
  68.                 ]
  69.             ]) 
  70.             ->add('message'TextareaType::class, [
  71.                 'required' => true,
  72.                 'constraints' => [
  73.                     new NotBlank(['message' => 'msg_required_msg']) 
  74.                 ],
  75.                 'attr' =>[
  76.                     'placeholder' => 'Your msg*',
  77.             ]
  78.             ])
  79.             ->add('isAccepted'CheckboxType::class, [
  80.                 'required' => true,
  81.                 'label'    => false,
  82.                 'constraints' => [
  83.                     new NotBlank(['message' => $this->translator->trans('form.accept_conditions',[],'forms')])  ,
  84.                 ]
  85.             ])
  86.         
  87.             ->add('email_honeypot'HoneypotType::class)
  88.             ->add('origin'HiddenType::class, [
  89.                 'data' => $options['origin'],
  90.                 'required' => false
  91.             ])
  92.             ->add('originId'HiddenType::class, [
  93.                 'data' => $options['originId'],
  94.                 'required' => false
  95.             ])
  96.             ->add('captchaVal'HiddenType::class , [
  97.                 'mapped' => false
  98.             ])->add('captchaUser'null , [
  99.                 'attr' => [
  100.                     'placeholder' => '?',
  101.                 ],
  102.                 'mapped' => false,
  103.             ])
  104.             ;
  105.         $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event)  {
  106.             $form $event->getForm();
  107.             $captchaUser $form->get('captchaUser')->getData();
  108.             $captchaVal $form->get('captchaVal')->getData();
  109.             $val=md5($captchaUser);
  110.             if ($val !== $captchaVal) {
  111.                 $form->get('captchaUser')->addError(new FormError($this->translator->trans('form.captcha',[],'front')));
  112.             }
  113.         });            
  114.     }
  115.     public function configureOptions(OptionsResolver $resolver): void
  116.     {
  117.         $resolver->setDefaults([
  118.             'data_class' => Contact::class,
  119.             'translation_domain' => 'forms',
  120.             'csrf_protection' => false,
  121.             'allow_extra_fields' => true,
  122.             'idLangue' => null,
  123.             'user' => null,
  124.             'origin' => null,
  125.             'originId' => null,
  126.         ]);
  127.     }
  128. }