src\Form\ContactType.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\FormBuilderInterface;
  5. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  6. use Symfony\Component\Form\Extension\Core\Type\TextType;
  7. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  8. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  9. use Symfony\Component\OptionsResolver\OptionsResolver;
  10. use Symfony\Component\Validator\Constraints\NotBlank;
  11. use Symfony\Component\Validator\Constraints\Length;
  12. use Symfony\Component\Validator\Constraints\Email;
  13. use App\Entity\Contact;
  14. class ContactType extends AbstractType
  15. {
  16. public function buildForm(FormBuilderInterface $builder, array $options): void
  17. {
  18. $builder
  19. ->add('email', EmailType::class, [
  20. 'label' => 'Email',
  21. 'attr' => ['placeholder' => 'Email'],
  22. 'required' => true,
  23. 'constraints' => [
  24. new NotBlank([
  25. 'message' => 'Ce champ ne peut être vide'
  26. ]),
  27. new Length([
  28. // max length allowed by Symfony for security reasons
  29. 'max' => 50,
  30. ]),
  31. new Email([
  32. 'message' => 'Veuillez entrer une adresse email valide',
  33. ]),
  34. ]
  35. ])
  36. ->add('prenom', TextType::class, [
  37. 'label' => 'Prénom',
  38. 'attr' => ['placeholder' => 'Prénom'],
  39. 'required' => true,
  40. 'constraints' => [
  41. new NotBlank([
  42. 'message' => 'Ce champ ne peut être vide',
  43. ]),
  44. new Length([
  45. // max length allowed by Symfony for security reasons
  46. 'max' => 50,
  47. ]),
  48. ],
  49. ])
  50. ->add('nom', TextType::class, [
  51. 'label' => 'Nom',
  52. 'attr' => ['placeholder' => 'Nom'],
  53. 'required' => true,
  54. 'constraints' => [
  55. new NotBlank([
  56. 'message' => 'Ce champ ne peut être vide',
  57. ]),
  58. new Length([
  59. // max length allowed by Symfony for security reasons
  60. 'max' => 50,
  61. ]),
  62. ],
  63. ])
  64. ->add('titre', TextType::class, [
  65. 'label' => 'Titre',
  66. 'attr' => ['placeholder' => 'Titre'],
  67. 'required' => true,
  68. 'constraints' => [
  69. new NotBlank([
  70. 'message' => 'Ce champ ne peut être vide',
  71. ]),
  72. new Length([
  73. // max length allowed by Symfony for security reasons
  74. 'max' => 50,
  75. ]),
  76. ],
  77. ])
  78. ->add('message', TextareaType::class, [
  79. 'label' => 'Message',
  80. 'attr' => ['placeholder' => 'Message'],
  81. 'required' => true,
  82. 'constraints' => [
  83. new NotBlank([
  84. 'message' => 'Ce champ ne peut être vide',
  85. ]),
  86. new Length([
  87. // max length allowed by Symfony for security reasons
  88. 'max' => 255,
  89. ]),
  90. ],
  91. ]);
  92. // Bouton Envoyer
  93. $builder->add('submit', SubmitType::class, array(
  94. 'label' => 'Envoyer'
  95. ));
  96. }
  97. public function configureOptions(OptionsResolver $resolver): void
  98. {
  99. $resolver->setDefaults([
  100. 'data_class' => Contact::class,
  101. ]);
  102. }
  103. }