src/Form/RegistrationFormType.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\User;
  4. use Symfony\Component\Form\AbstractType;
  5. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  6. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  7. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  8. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  9. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  10. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  11. use Symfony\Component\Form\Extension\Core\Type\TextType;
  12. use Symfony\Component\Form\FormBuilderInterface;
  13. use Symfony\Component\OptionsResolver\OptionsResolver;
  14. use Symfony\Component\Validator\Constraints\IsTrue;
  15. use Symfony\Component\Validator\Constraints\Length;
  16. use Symfony\Component\Validator\Constraints\NotBlank;
  17. class RegistrationFormType extends AbstractType
  18. {
  19.     public function buildForm(FormBuilderInterface $builder, array $options): void
  20.     {
  21.         $builder
  22.             ->add('email'EmailType::class, [
  23.                 'required' => true,
  24.                 'label' => 'Emailová adresa *',
  25.                 'row_attr' => [
  26.                     'class' => 'form-floating mb-3',
  27.                 ],
  28.                 'constraints' => [
  29.                     new NotBlank([
  30.                         'message' => 'Vyplňte emailovou adresu',
  31.                     ]),
  32.                 ],
  33.             ])
  34.             ->add('plainPassword'RepeatedType::class, [
  35.                 'type' => PasswordType::class,
  36.                 'first_options'  => [
  37.                     'label' => 'Heslo *',
  38.                     'row_attr' => [
  39.                         'class' => 'form-floating mb-3',
  40.                     ],
  41.                 ],
  42.                 'second_options' => [
  43.                     'label' => 'Zopakujte heslo *',
  44.                     'row_attr' => [
  45.                         'class' => 'form-floating mb-3',
  46.                     ],
  47.                 ],
  48.                 'mapped' => false,
  49.                 'required' => true,
  50.                 'attr' => ['autocomplete' => 'new-password'],
  51.                 'constraints' => [
  52.                     new NotBlank([
  53.                         'message' => 'Vyplňte heslo',
  54.                     ]),
  55.                     new Length([
  56.                         'min' => 6,
  57.                         'minMessage' => 'Heslo musí obsahovat minimálně {{ limit }} znaků',
  58.                         // max length allowed by Symfony for security reasons
  59.                         'max' => 100,
  60.                         'maxMessage' => 'Heslo může obsahovat maxmimálně {{ limit }} znaků',
  61.                     ]),
  62.                 ],
  63.             ])
  64.             ->add('firstname'TextType::class, [
  65.                 'required' => true,
  66.                 'label' => 'Jméno *',
  67.                 'row_attr' => [
  68.                     'class' => 'form-floating mb-3',
  69.                 ],
  70.                 'constraints' => [
  71.                     new NotBlank([
  72.                         'message' => 'Vyplňte jméno',
  73.                     ]),
  74.                 ],
  75.             ])
  76.             ->add('lastname'TextType::class, [
  77.                 'required' => true,
  78.                 'label' => 'Příjmení *',
  79.                 'row_attr' => [
  80.                     'class' => 'form-floating mb-3',
  81.                 ],
  82.                 'constraints' => [
  83.                     new NotBlank([
  84.                         'message' => 'Vyplňte příjmení',
  85.                     ]),
  86.                 ],
  87.             ])
  88.             ->add('phone'NumberType::class, [
  89.                 'required' => true,
  90.                 'row_attr' => [
  91.                     'class' => 'form-floating mb-3',
  92.                 ],
  93.                 'constraints' => [
  94.                     new NotBlank([
  95.                         'message' => 'Vyplňte telefonní číslo',
  96.                     ]),
  97.                 ],
  98.                 'label' => 'Telefonní číslo *',
  99.             ])
  100.             ->add('country'TextType::class, [
  101.                 'required' => true,
  102.                 'row_attr' => [
  103.                     'class' => 'form-floating mb-3',
  104.                 ],
  105.                 'constraints' => [
  106.                     new NotBlank([
  107.                         'message' => 'Vyplňte zemi',
  108.                     ]),
  109.                 ],
  110.                 'label' => 'Země *',
  111.             ])
  112.             ->add('city'TextType::class, [
  113.                 'required' => true,
  114.                 'row_attr' => [
  115.                     'class' => 'form-floating mb-3',
  116.                 ],
  117.                 'constraints' => [
  118.                     new NotBlank([
  119.                         'message' => 'Vyplňte město',
  120.                     ]),
  121.                 ],
  122.                 'label' => 'Město *',
  123.             ])
  124.             ->add('zipcode'TextType::class, [
  125.                 'required' => true,
  126.                 'row_attr' => [
  127.                     'class' => 'form-floating mb-3',
  128.                 ],
  129.                 'constraints' => [
  130.                     new NotBlank([
  131.                         'message' => 'Vyplňte PSČ',
  132.                     ]),
  133.                 ],
  134.                 'label' => 'PSČ *',
  135.             ])
  136.             ->add('street'TextType::class, [
  137.                 'required' => true,
  138.                 'row_attr' => [
  139.                     'class' => 'form-floating mb-3',
  140.                 ],
  141.                 'constraints' => [
  142.                     new NotBlank([
  143.                         'message' => 'Vyplňte ulici',
  144.                     ]),
  145.                 ],
  146.                 'label' => 'Ulice *',    
  147.             ])
  148.             ->add('street2'TextType::class, [
  149.                 'label' => 'Apt. nr., floor',
  150.                 'row_attr' => [
  151.                     'class' => 'form-floating mb-3',
  152.                 ],
  153.                 'required' => false,
  154.             ])
  155.             ->add('terms'CheckboxType::class, [
  156.                 'mapped' => false,
  157.                 'constraints' => [
  158.                     new IsTrue([
  159.                         'message' => 'Pro registraci musíte souhlasit s podmínkami.',
  160.                     ]),
  161.                 ],
  162.                 'required' => true,
  163.                 'label' => 'Souhlasím s <a href="{{ path(\'app_terms\') }}">podmínkami</a> používání této služby.',
  164.                 'label_html' => true
  165.             ])
  166.             ->add('save'SubmitType::class, [
  167.                 'label' => 'Registrovat se'
  168.             ])
  169.         ;
  170.     }
  171.     public function configureOptions(OptionsResolver $resolver): void
  172.     {
  173.         $resolver->setDefaults([
  174.             'data_class' => User::class,
  175.             // enable/disable CSRF protection for this form
  176.             'csrf_protection' => true,
  177.             // the name of the hidden HTML field that stores the token
  178.             'csrf_field_name' => '_token',
  179.             // an arbitrary string used to generate the value of the token
  180.             // using a different string for each form improves its security
  181.             'csrf_token_id'   => 'task_item',
  182.         ]);
  183.     }
  184. }