src/Form/RegisterType.php line 21

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\EmailType;
  6. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  7. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  8. use Symfony\Component\Form\Extension\Core\Type\TextType;
  9. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  10. use Symfony\Component\Form\FormBuilderInterface;
  11. use Symfony\Component\OptionsResolver\OptionsResolver;
  12. use Symfony\Component\Validator\Constraints\Email;
  13. use Symfony\Component\Validator\Constraints\Regex;
  14. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  15. use Symfony\Component\Validator\Constraints\Callback;
  16. use Symfony\Component\Validator\Constraints\Length;
  17. use Symfony\Component\Validator\Constraints\NotBlank;
  18. class RegisterType extends AbstractType
  19. {
  20.     public function buildForm(FormBuilderInterface $builder, array $options)
  21.     {
  22.         $builder
  23.             ->add('firstName'TextType::class, [
  24.                 'label' => 'label.your_firstname',
  25.             ])
  26.             ->add('lastName'TextType::class, [
  27.                 'label' => 'label.your_lastname',
  28.             ])
  29.             ->add('email'EmailType::class, [
  30.                 'label' => 'label.your_email',
  31.                 'attr'  => [
  32.                     'placeholder' => 'user@mail.ru',
  33.                     'class' => 'disabled',
  34.                 ],
  35.                 'constraints' => [
  36. //                    new Callback([
  37. //                        'callback' => function ($value, ExecutionContextInterface $context) {
  38. //                            $parts = explode('@', $value);
  39. //                            if (!in_array($parts[1], ['yandex.ru', 'mail.ru', 'rambler.ru'])) {
  40. //                                $context
  41. //                                    ->buildViolation('Для регистрации можно использовать только российские сервисы (yandex.ru, mail.ru, rambler.ru)')
  42. //                                    ->addViolation();
  43. //                            }
  44. //                        }
  45. //                    ])
  46.                 ]
  47.             ])
  48.             ->add('password'RepeatedType::class, [
  49.                 'type' => PasswordType::class,
  50.                 'first_options' => [
  51.                     'attr' => ['autocomplete' => 'new-password'],
  52.                     'constraints' => [
  53.                         new NotBlank([
  54.                             'message' => 'Введите пароль',
  55.                         ]),
  56.                         new Length([
  57.                             'min' => 8,
  58.                             'minMessage' => 'Минимальная длина пароля {{ limit }} символов',
  59.                             'max' => 4096,
  60.                         ]),
  61.                         new Regex([
  62.                             'pattern' => '/^[A-Za-z0-9\!\#\$\%\&\+\-\.\<\=\>\?\^\_]+$/',
  63.                             'message' => 'Допустимые символы: A-Z, a-z, 0-9, !, #, $, %, &, + , -, (точка), <, =, >, ?, ^, _'
  64.                         ])
  65.                     ],
  66.                     'label' => 'label.create_pass',
  67.                 ],
  68.                 'second_options' => [
  69.                     'attr' => ['autocomplete' => 'new-password'],
  70.                     'label' => 'label.repeat_pass',
  71.                 ],
  72.                 'invalid_message' => 'Пароли не совпадают',
  73.                 'mapped' => false,
  74.             ])
  75.             ->add('agree'CheckboxType::class, [
  76.                 'label' => 'label.agree_with_terms',
  77.                 'constraints' => [
  78.                     new NotBlank([
  79.                         'message' => 'Вы должны согласиться с условиями',
  80.                     ]),
  81.                 ],
  82.                 'mapped' => false,
  83.                 'attr' => [
  84.                     'checked' => true
  85.                 ]
  86.             ]);
  87.     }
  88.     public function configureOptions(OptionsResolver $resolver)
  89.     {
  90.         $resolver->setDefaults([
  91.             'data_class' => User::class,
  92.         ]);
  93.     }
  94. }