src/Form/Auth/RegisterFormType.php line 42

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore Customer Management Framework Bundle
  4.  * Full copyright and license information is available in
  5.  * LICENSE.md which is distributed with this source code.
  6.  *
  7.  * @copyright  Copyright (C) Elements.at New Media Solutions GmbH
  8.  * @license    GPLv3
  9.  */
  10. namespace App\Form\Auth;
  11. use App\Form\DefaultFormType;
  12. use App\Form\Type\PimcoreCountryType;
  13. use Pimcore\Model\DataObject\Data\Consent;
  14. use Symfony\Component\Form\CallbackTransformer;
  15. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  16. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  17. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  18. use Symfony\Component\Form\Extension\Core\Type\FormType;
  19. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  20. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  21. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  22. use Symfony\Component\Form\Extension\Core\Type\TextType;
  23. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  24. use Symfony\Component\Form\FormBuilderInterface;
  25. use Symfony\Component\OptionsResolver\OptionsResolver;
  26. use Symfony\Component\Validator\Constraints\Length;
  27. use Symfony\Component\Validator\Constraints\NotBlank;
  28. use Symfony\Component\Validator\Constraints\Email;
  29. use Symfony\Component\Validator\Constraints\Regex;
  30. use Pimcore\Model\DataObject\CountryAndRegions;
  31. use Symfony\Component\Form\FormInterface;
  32. use Pimcore\Model\DataObject\Customer;
  33. class RegisterFormType extends DefaultFormType
  34. {
  35.     /**
  36.      * @inheritDoc
  37.      */
  38.     public function buildForm(FormBuilderInterface $builder, array $options)
  39.     {
  40.         $translator $this->translator;
  41.         $customer = new Customer();
  42.         $prefixes $customer->getClass()->getFieldDefinition('prefix');
  43.         $prefixOptions = [ $translator->trans('stoelting.forms.prefix.placeholder') => '' ];
  44.         foreach ($prefixes->getOptions() as $option) {
  45.             $prefixOptions[$option['key']] = $option['value'];
  46.         }
  47.         $builder->add(
  48.             'accountInformation',
  49.             FormType::class,
  50.             [
  51.                 'label' => $translator->trans('stoelting.register.accountInformation-title'),
  52.             ]
  53.         );
  54.         $builder->add(
  55.             'address',
  56.             FormType::class,
  57.             [
  58.                 'label' => $translator->trans('stoelting.register.address-title'),
  59.             ]
  60.         );
  61.         $builder->get('accountInformation')->add(
  62.             'email',
  63.             EmailType::class,
  64.             [
  65.                 'label'       => $this->translator->trans('stoelting.email'),
  66.                 'attr'        => [
  67.                     'placeholder' => $this->translator->trans('stoelting.email.placeholder'),
  68.                 ],
  69.                 'constraints' => [
  70.                     new NotBlank([
  71.                         'message' => $translator->trans('stoelting.email.required.message'),
  72.                     ]),
  73.                     new Email([
  74.                         'message' => $translator->trans('stoelting.invalid-email'),
  75.                         'mode' => 'strict'
  76.                     ])
  77.                 ]
  78.             ]
  79.         )->add(
  80.             'password',
  81.             RepeatedType::class,
  82.             [
  83.                 'type'            => PasswordType::class,
  84.                 'invalid_message' => $translator->trans('stoelting.passwords-dont-match.required.message'),
  85.                 'constraints'     => [
  86.                     new NotBlank(
  87.                         [
  88.                             'message' => $translator->trans('stoelting.password.required.message'),
  89.                         ]
  90.                     ),
  91.                     new Length(
  92.                         [
  93.                             'min' => 8,
  94.                             'minMessage' => $translator->trans('stoelting.password.min.message'),
  95.                         ]
  96.                     )
  97.                 ],
  98.                 'first_options'   => [
  99.                     'label' => $this->translator->trans('stoelting.password'),
  100.                     'attr'  => [
  101.                         'placeholder' => $this->translator->trans(
  102.                             'stoelting.password.placeholder'
  103.                         )
  104.                     ]
  105.                 ],
  106.                 'second_options'  => [
  107.                     'label' => $this->translator->trans('stoelting.password-confirmation'),
  108.                     'attr'  => [
  109.                         'placeholder' => $this->translator->trans(
  110.                             'stoelting.password-confirmation.placeholder'
  111.                         )
  112.                     ]
  113.                 ],
  114.             ]
  115.         )->add(
  116.             'prefix',
  117.             ChoiceType::class,
  118.             [
  119.                 'label'    => $translator->trans('stoelting.prefix'),
  120.                 'placeholder' => $translator->trans('stoelting.prefix.placeholder'),
  121.                 'multiple' => false,
  122.                 'choices'  => $prefixOptions,
  123.                 'choice_attr' => function ($key$val$index) {
  124.                     return empty($key) ? ['disabled' => 'disabled'] : [];
  125.                 }
  126.             ]
  127.         )->add(
  128.             'firstname',
  129.             TextType::class,
  130.             [
  131.                 'label'       => $this->translator->trans('stoelting.firstname'),
  132.                 'attr'        => [
  133.                     'placeholder' => $this->translator->trans('stoelting.firstname.placeholder'),
  134.                 ],
  135.                 'constraints' => [
  136.                     new NotBlank(
  137.                         [
  138.                             'message' => $translator->trans('stoelting.firstname.required.message'),
  139.                         ]
  140.                     )
  141.                 ]
  142.             ]
  143.         )->add(
  144.             'lastname',
  145.             TextType::class,
  146.             [
  147.                 'label'       => $this->translator->trans('stoelting.lastname'),
  148.                 'attr'        => [
  149.                     'placeholder' => $this->translator->trans('stoelting.lastname.placeholder'),
  150.                 ],
  151.                 'constraints' => [
  152.                     new NotBlank(
  153.                         [
  154.                             'message' => $translator->trans('stoelting.lastname.required.message'),
  155.                         ]
  156.                     )
  157.                 ]
  158.             ]
  159.         )->add(
  160.             'divisions',
  161.             ChoiceType::class,
  162.             [
  163.                 'label'    => $translator->trans('stoelting.divisions'),
  164.                 'attr'     => [
  165.                     'placeholder' => $translator->trans('stoelting.divisions.select-area'),
  166.                 ],
  167.                 'multiple' => true,
  168.                 'choices'  => [
  169.                     'Neuroscience/ANY-maze' => 'neuroscience',
  170.                     'Polygraph Instruments' => 'polygraph-instruments',
  171.                     'Psychological Testing' => 'psychological-testing'
  172.                 ],
  173.                 'constraints' => [
  174.                     new NotBlank(
  175.                         [
  176.                             'message' => $translator->trans('stoelting.divisions.required.message'),
  177.                         ]
  178.                     )
  179.                 ]
  180.             ]
  181.         )->add(
  182.             'newsletter',
  183.             CheckboxType::class,
  184.             [
  185.                 'label' => $translator->trans('stoelting.should-contact'),
  186.             ]
  187.         );
  188.         $builder->get('accountInformation')->get('newsletter')->addModelTransformer(
  189.             new CallbackTransformer(
  190.                 function ($newsletter) {
  191.                     return $newsletter;
  192.                 },
  193.                 function ($newsletter) {
  194.                     return new Consent($newsletter);
  195.                 }
  196.             )
  197.         );
  198.         $builder->get('address')->add(
  199.             'company',
  200.             TextType::class,
  201.             [
  202.                 'label' => $translator->trans('stoelting.company'),
  203.                 'attr'  => [
  204.                     'placeholder' => $translator->trans('stoelting.enter-company'),
  205.                 ]
  206.             ]
  207.         )->add(
  208.             'streetLine01',
  209.             TextType::class,
  210.             [
  211.                 'label'       => $translator->trans('stoelting.StreetLine02-required'),
  212.                 'attr'        => [
  213.                     'placeholder' => $translator->trans('stoelting.enter-street'),
  214.                 ],
  215.                 'constraints' => [
  216.                     new NotBlank(
  217.                         [
  218.                             'message' => $translator->trans('stoelting.street-required.message'),
  219.                         ]
  220.                     )
  221.                 ]
  222.             ]
  223.         )->add(
  224.             'streetLine02',
  225.             TextType::class,
  226.             [
  227.                 'label' => $translator->trans('stoelting.second-street'),
  228.                 'attr'  => [
  229.                     'placeholder' => $translator->trans('stoelting.enter-second-street'),
  230.                 ],
  231.             ]
  232.         )->add(
  233.             'city',
  234.             TextType::class,
  235.             [
  236.                 'label'       => $translator->trans('stoelting.city-required'),
  237.                 'attr'        => [
  238.                     'placeholder' => $translator->trans('stoelting.enter-city'),
  239.                 ],
  240.                 'constraints' => [
  241.                     new NotBlank(
  242.                         [
  243.                             'message' => $translator->trans('stoelting.city-required.message'),
  244.                         ]
  245.                     )
  246.                 ]
  247.             ]
  248.         )->add(
  249.             'state',
  250.             TextType::class,
  251.             [
  252.                 'label' => $translator->trans('stoelting.state'),
  253.                 'attr'  => [
  254.                     'placeholder' => $translator->trans('stoelting.state'),
  255.                     'class' => 'input-state-name'
  256.                 ],
  257.                 'constraints' => [
  258.                     new NotBlank(
  259.                         [
  260.                             'groups' => ['required_state'],
  261.                             'message' => $translator->trans('stoelting.state-required.message'),
  262.                         ]
  263.                     )
  264.                 ]
  265.             ]
  266.         )->add(
  267.             'stateDisplay',
  268.             HiddenType::class,
  269.             [
  270.                 'label' => false,
  271.                 'attr'  => [
  272.                     'class' => 'input-state-display'
  273.                 ],
  274.             ]
  275.         )->add(
  276.             'zipCode',
  277.             TextType::class,
  278.             [
  279.                 'label'       => $translator->trans('stoelting.zip-required'),
  280.                 'attr'        => [
  281.                     'placeholder' => $translator->trans('stoelting.zip-street'),
  282.                 ],
  283.                 'constraints' => [
  284.                     new NotBlank(
  285.                         [
  286.                             'message' => $translator->trans('stoelting.zip-required.message'),
  287.                         ]
  288.                     )
  289.                 ]
  290.             ]
  291.         )->add(
  292.             'country',
  293.             PimcoreCountryType::class,
  294.             [
  295.                 'label'       => $translator->trans('stoelting.country-required'),
  296.                 'attr'        => [
  297.                     'placeholder' => $translator->trans('stoelting.country'),
  298.                     'class' => 'input-country-select'
  299.                 ],
  300.                 'constraints' => [
  301.                     new NotBlank(
  302.                         [
  303.                             'message' => $translator->trans('stoelting.country-required.message'),
  304.                         ]
  305.                     )
  306.                 ]
  307.             ]
  308.         )->add(
  309.             'phone',
  310.             TextType::class,
  311.             [
  312.                 'label'       => $translator->trans('stoelting.phone-required'),
  313.                 'attr'        => [
  314.                     'placeholder' => $translator->trans('stoelting.enter-phone-number'),
  315.                 ],
  316.                 'constraints' => [
  317.                     new Regex(
  318.                         [
  319.                             'pattern' => '/\+?[0-9]/',
  320.                             'message' => $translator->trans('stoelting.invalid-phone-number'),
  321.                         ]
  322.                     )
  323.                 ]
  324.             ]
  325.         )->add(
  326.             'fax',
  327.             TextType::class,
  328.             [
  329.                 'label' => $translator->trans('stoelting.fax'),
  330.                 'attr'  => [
  331.                     'placeholder' => $translator->trans('stoelting.enter-fax-number'),
  332.                 ],
  333.             ]
  334.         );
  335.         $builder->get('accountInformation')->add(
  336.             'VATNumber',
  337.             TextType::class,
  338.             [
  339.                 'label' => $translator->trans('stoelting.tax-exempt-number'),
  340.                 'attr'  => [
  341.                     'placeholder' => $translator->trans('stoelting.enter-tax-exempt-number'),
  342.                 ],
  343.             ]
  344.         );
  345.         $builder
  346.             ->add('g-recaptcha-response'HiddenType::class, [
  347.                 'attr' => [
  348.                     'id' => 'g-recaptcha-response'
  349.                 ],
  350.             ])
  351.             ->add('action'HiddenType::class, [
  352.                 'attr' => [
  353.                     'id' => 'validate_captcha'
  354.                 ],
  355.                 'data' => 'registerForm'
  356.             ])
  357.             ->add(
  358.             'save',
  359.             SubmitType::class,
  360.             [
  361.                 'label' => $this->translator->trans('stoelting.register-submit'),
  362.                 'attr'  => [
  363.                     'class' => 'btn btn-primary'
  364.                 ]
  365.             ]
  366.         );
  367.     }
  368.     /**
  369.      * @inheritDoc
  370.      */
  371.     public function getBlockPrefix()
  372.     {
  373.         return '';
  374.     }
  375.     /**
  376.      * @inheritDoc
  377.      */
  378.     public function configureOptions(OptionsResolver $resolver)
  379.     {
  380.         $resolver->setDefaults([
  381.             'validation_groups' => function (FormInterface $form) {
  382.                 $data $form->getData();
  383.                 $address $data['address'];
  384.                 $countryCode $address['country'] ?? null;
  385.                 if ($countryCode) {
  386.                     $country CountryAndRegions::getByPath(CountryAndRegions::FOLDER_PATH.$countryCode);
  387.                     if ($country && $country->getIsRegionRequired()) {
  388.                         return ['Default''required_state'];
  389.                     }
  390.                 }
  391.                 return ['Default'];
  392.             },
  393.         ]);
  394.     }
  395. }