src/Form/ContactFormType.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Model\Contact;
  4. use Symfony\Component\Form\AbstractType;
  5. use Symfony\Component\Form\FormBuilderInterface;
  6. use Symfony\Component\OptionsResolver\OptionsResolver;
  7. use Symfony\Component\Validator\Constraints\NotBlank;
  8. use Symfony\Component\Form\Extension\Core\Type\TextType;
  9. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  10. use Symfony\Component\Validator\Constraints\Email;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. class ContactFormType extends AbstractType
  13. {
  14. private $translator;
  15. public function __construct(TranslatorInterface $translator)
  16. {
  17. $this->translator = $translator;
  18. }
  19. public function buildForm(FormBuilderInterface $builder, array $options): void
  20. {
  21. $builder
  22. ->add('name', TextType::class, [
  23. "label" => $this->translator->trans("Votre nom"),
  24. "attr" => ["placeholder" => $this->translator->trans("Nom")." *"],
  25. "trim" => true,
  26. "required" => true,
  27. "constraints" => [
  28. new NotBlank(["message" => "Nom obligatoire"])
  29. ]
  30. ])
  31. ->add('email', TextType::class, [
  32. "label" => $this->translator->trans("Votre adresse email"),
  33. "attr" => ["placeholder" => $this->translator->trans("Adresse email"). " *"],
  34. "trim" => true,
  35. "required" => true,
  36. "constraints" => [
  37. new NotBlank(["message" => "Adresse email obligatoire"]),
  38. new Email(["message" => "Adresse email invalide"])
  39. ]
  40. ])
  41. ->add('subject', TextType::class, [
  42. "label" => $this->translator->trans("Sujet"),
  43. "attr" => ["placeholder" => $this->translator->trans("Sujet")." *"],
  44. "trim" => true,
  45. "required" => true,
  46. "constraints" => [
  47. new NotBlank(["message" => $this->translator->trans("Sujet obligatoire")])
  48. ]
  49. ])
  50. ->add('message', TextareaType::class, [
  51. "label" => $this->translator->trans("Votre message"),
  52. "attr" => ["placeholder" => "Message *"],
  53. "trim" => true,
  54. "required" => true,
  55. "constraints" => [
  56. new NotBlank(["message" => $this->translator->trans("Message obligatoire")])
  57. ]
  58. ])
  59. ;
  60. }
  61. public function configureOptions(OptionsResolver $resolver): void
  62. {
  63. $resolver->setDefaults([
  64. 'data_class' => Contact::class,
  65. ]);
  66. }
  67. }