<?php
namespace App\Form;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
class RegisterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstName', TextType::class, [
'label' => 'label.your_firstname',
])
->add('lastName', TextType::class, [
'label' => 'label.your_lastname',
])
->add('email', EmailType::class, [
'label' => 'label.your_email',
'attr' => [
'placeholder' => 'user@mail.ru',
'class' => 'disabled',
],
'constraints' => [
// new Callback([
// 'callback' => function ($value, ExecutionContextInterface $context) {
// $parts = explode('@', $value);
// if (!in_array($parts[1], ['yandex.ru', 'mail.ru', 'rambler.ru'])) {
// $context
// ->buildViolation('Для регистрации можно использовать только российские сервисы (yandex.ru, mail.ru, rambler.ru)')
// ->addViolation();
// }
// }
// ])
]
])
->add('password', RepeatedType::class, [
'type' => PasswordType::class,
'first_options' => [
'attr' => ['autocomplete' => 'new-password'],
'constraints' => [
new NotBlank([
'message' => 'Введите пароль',
]),
new Length([
'min' => 8,
'minMessage' => 'Минимальная длина пароля {{ limit }} символов',
'max' => 4096,
]),
new Regex([
'pattern' => '/^[A-Za-z0-9\!\#\$\%\&\+\-\.\<\=\>\?\^\_]+$/',
'message' => 'Допустимые символы: A-Z, a-z, 0-9, !, #, $, %, &, + , -, (точка), <, =, >, ?, ^, _'
])
],
'label' => 'label.create_pass',
],
'second_options' => [
'attr' => ['autocomplete' => 'new-password'],
'label' => 'label.repeat_pass',
],
'invalid_message' => 'Пароли не совпадают',
'mapped' => false,
])
->add('agree', CheckboxType::class, [
'label' => 'label.agree_with_terms',
'constraints' => [
new NotBlank([
'message' => 'Вы должны согласиться с условиями',
]),
],
'mapped' => false,
'attr' => [
'checked' => true
]
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}