src/Form/RegistrationType.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\User;
  4. use App\Service\TextResolverService;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  7. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  8. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  9. use Symfony\Component\Form\Extension\Core\Type\TextType;
  10. use Symfony\Component\Form\FormBuilderInterface;
  11. use Symfony\Component\OptionsResolver\OptionsResolver;
  12. use Symfony\Component\Validator\Constraints\Length;
  13. use Symfony\Component\Validator\Constraints\NotBlank;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. class RegistrationType extends AbstractType {
  16. /** @var TranslatorInterface */
  17. private $translator;
  18. /** @var TextResolverService */
  19. private $textResolver;
  20. public function __construct(TranslatorInterface $translator, TextResolverService $textResolver) {
  21. $this->translator = $translator;
  22. $this->textResolver = $textResolver;
  23. }
  24. public function buildForm(FormBuilderInterface $builder, array $options): void {
  25. $builder
  26. ->add('email', TextType::class, [
  27. 'attr' => [
  28. 'required' => true,
  29. ],
  30. 'disabled' => $options['address_update'],
  31. ])
  32. ->add('firstname', TextType::class, [
  33. 'attr' => [
  34. 'required' => true,
  35. ],
  36. 'disabled' => $options['address_update'],
  37. ])
  38. ->add('lastname', TextType::class, [
  39. 'attr' => [
  40. 'required' => true,
  41. ],
  42. 'disabled' => $options['address_update'],
  43. ]);
  44. if (!$options['address_update']) {
  45. $builder->add('plainPassword', PasswordType::class, [
  46. // instead of being set onto the object directly,
  47. // this is read and encoded in the controller
  48. 'mapped' => false,
  49. 'attr' => ['autocomplete' => 'new-password'],
  50. 'constraints' => [
  51. new NotBlank([
  52. 'message' => $this->translator->trans('form.field.password.not_blank'),
  53. ]),
  54. new Length([
  55. 'min' => 6,
  56. 'minMessage' => $this->translator->trans('form.field.password.too_short'),
  57. // max length allowed by Symfony for security reasons
  58. 'max' => 4096,
  59. ]),
  60. ],
  61. ]);
  62. }
  63. $builder->add('addressBook', AddressBookType::class, [
  64. 'required' => false,
  65. 'enable_customer_number' => $options['enable_customer_number'],
  66. ]);
  67. $builder->add('agreeToTiInfos', CheckboxType::class, [
  68. 'label' => $this->resolveAndSanitizeLabel(),
  69. 'label_html' => true,
  70. 'required' => false,
  71. ]);
  72. $submitLabel = 'Registrieren';
  73. if ($options['address_update']) {
  74. $submitLabel = 'Speichern';
  75. }
  76. $builder->add('submit', SubmitType::class, [
  77. 'attr' => [
  78. 'class' => 'button accent w-100 mt-5',
  79. ],
  80. 'label' => $submitLabel,
  81. ]);
  82. }
  83. public function configureOptions(OptionsResolver $resolver): void {
  84. $resolver->setDefaults([
  85. 'data_class' => User::class,
  86. 'during_order' => false,
  87. 'address_update' => false,
  88. 'enable_customer_number' => false,
  89. ]);
  90. }
  91. private function resolveAndSanitizeLabel(): ?string {
  92. $text = strip_tags($this->textResolver->resolve('register.page.box.newsletter.consent'), '<a>');
  93. return preg_replace_callback(
  94. '/<a\s+[^>]*href=["\']([^"\']+)["\'][^>]*>/i',
  95. function ($matches) {
  96. $url = $matches[1];
  97. // Nur http und https erlauben
  98. if (!preg_match('#^https?://#i', $url)) {
  99. return ''; // Link entfernen, wenn unsicher
  100. }
  101. // Link sauber neu aufbauen (nur href)
  102. return '<a href="' . htmlspecialchars($url, ENT_QUOTES, 'UTF-8') . '" target="_blank" rel="noopener noreferrer">';
  103. },
  104. $text
  105. );
  106. }
  107. }