src/Form/Type/ContactType.php line 30

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Sylius package.
  4. *
  5. * (c) Paweł Jędrzejewski
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace App\Form\Type;
  12. use Symfony\Component\Form\AbstractType;
  13. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  14. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  15. use FOS\CKEditorBundle\Form\Type\CKEditorType;
  16. use Symfony\Component\Form\FormBuilderInterface;
  17. use Symfony\Component\Form\FormEvent;
  18. use Symfony\Component\Form\FormEvents;
  19. use Symfony\Component\OptionsResolver\OptionsResolver;
  20. use Symfony\Component\Validator\Constraints\Email;
  21. use Symfony\Component\Validator\Constraints\NotBlank;
  22. use App\Form\Type\EWZRecaptchaType;
  23. use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaV3Type;
  24. use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrueV3;
  25. final class ContactType extends AbstractType
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function buildForm(FormBuilderInterface $builder, array $options): void
  31. {
  32. $builder
  33. ->add('email', EmailType::class, [
  34. 'label' => 'sylius.ui.email',
  35. 'constraints' => [
  36. new NotBlank([
  37. 'message' => 'sylius.contact.email.not_blank',
  38. ]),
  39. new Email([
  40. 'message' => 'sylius.contact.email.invalid',
  41. ]),
  42. ],
  43. ])
  44. ->add('message', CKEditorType::class, [
  45. 'label' => 'sylius.ui.message',
  46. 'config' => array('skin' => "moono_blue,/build/skins/moono_blue/"),
  47. 'constraints' => [
  48. new NotBlank([
  49. 'message' => 'sylius.contact.message.not_blank',
  50. ]),
  51. ],
  52. ])
  53. ->add('recaptcha', EWZRecaptchaV3Type::class, array(
  54. 'action_name' => 'register',
  55. 'constraints' => array(
  56. new IsTrueV3()
  57. )))
  58. ->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options): void {
  59. $email = $options['email'];
  60. if (null === $email) {
  61. return;
  62. }
  63. $data = $event->getData();
  64. $data['email'] = $email;
  65. $event->setData($data);
  66. })
  67. ;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function configureOptions(OptionsResolver $resolver): void
  73. {
  74. $resolver
  75. ->setDefaults([
  76. 'email' => null,
  77. ])
  78. ;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function getBlockPrefix(): string
  84. {
  85. return 'sylius_contact';
  86. }
  87. }