src/Form/ContactFormType.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Brand;
  4. use App\Request\ContactFormRequest;
  5. use Karser\Recaptcha3Bundle\Form\Recaptcha3Type;
  6. use Karser\Recaptcha3Bundle\Validator\Constraints\Recaptcha3;
  7. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  8. use Symfony\Component\Form\AbstractType;
  9. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  10. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  11. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  12. use Symfony\Component\Form\FormBuilderInterface;
  13. use Symfony\Component\OptionsResolver\OptionsResolver;
  14. use Symfony\Component\Validator\Constraints\Email;
  15. use Symfony\Component\Validator\Constraints\NotNull;
  16. class ContactFormType extends AbstractType
  17. {
  18. public function buildForm(FormBuilderInterface $builder, array $options)
  19. {
  20. $builder
  21. ->add('name', null, [
  22. 'required' => true,
  23. 'attr' => [
  24. 'placeholder' => 'Your Name',
  25. 'data-rule' => 'minlen:4',
  26. 'data-msg' => 'Please enter at least 4 chars',
  27. ],
  28. 'constraints' => [
  29. new NotNull(),
  30. ],
  31. ])
  32. ->add('brand', EntityType::class, [
  33. 'class' => Brand::class,
  34. 'choice_label' => 'name',
  35. 'required' => true,
  36. 'placeholder' => 'Select a Brand',
  37. 'attr' => [
  38. 'data-rule' => 'required',
  39. 'data-msg' => 'Please select a brand',
  40. ],
  41. 'constraints' => [
  42. new NotNull(),
  43. ],
  44. ])
  45. ->add('email', EmailType::class, [
  46. 'required' => true,
  47. 'attr' => [
  48. 'placeholder' => 'Your Email',
  49. 'data-rule' => 'email',
  50. 'data-msg' => 'Please enter a valid email',
  51. ],
  52. 'constraints' => [
  53. new NotNull(),
  54. new Email(['message' => 'Please enter a valid email']),
  55. ],
  56. ])
  57. ->add('message', TextareaType::class, [
  58. 'required' => true,
  59. 'attr' => [
  60. 'placeholder' => 'Message',
  61. 'data-rule' => 'required',
  62. 'data-msg' => 'Please write something for us',
  63. 'rows' => 5,
  64. 'help' => 'Please provide as much detailed information as possible regarding your support '.
  65. 'or technical issue.',
  66. ],
  67. 'constraints' => [
  68. new NotNull(['message' => 'Please write something for us']),
  69. ],
  70. 'help' => 'Please provide as much detailed information as possible regarding your support '.
  71. 'or technical issue.',
  72. ])
  73. ->add('captcha', Recaptcha3Type::class, [
  74. 'constraints' => new Recaptcha3(),
  75. 'action_name' => 'homepage',
  76. // 'script_nonce_csp' => $nonceCSP,
  77. ])
  78. ->add('submit', SubmitType::class)
  79. ;
  80. }
  81. public function configureOptions(OptionsResolver $resolver)
  82. {
  83. $resolver->setDefaults([
  84. 'data_class' => ContactFormRequest::class,
  85. ]);
  86. }
  87. }