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 $translatorTextResolverService $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.         ]);
  66.         $builder->add('agreeToTiInfos'CheckboxType::class, [
  67.             'label' => strip_tags($this->textResolver->resolve('register.page.box.newsletter.consent')),
  68.             'required' => false,
  69.         ]);
  70.         $submitLabel 'Registrieren';
  71.         if ($options['address_update']) {
  72.             $submitLabel 'Speichern';
  73.         }
  74.         $builder->add('submit'SubmitType::class, [
  75.             'attr' => [
  76.                 'class' => 'button accent w-100 mt-5',
  77.             ],
  78.             'label' => $submitLabel,
  79.         ]);
  80.     }
  81.     public function configureOptions(OptionsResolver $resolver): void {
  82.         $resolver->setDefaults([
  83.             'data_class' => User::class,
  84.             'during_order' => false,
  85.             'address_update' => false,
  86.         ]);
  87.     }
  88. }