<?php
namespace App\Form;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use App\Validator\Constraints\ValidUsername;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
class RegistrationFormType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options): void {
$builder
->add('username', TextType::class, [
'attr' => [
'class' => 'loginInput',
],
'constraints' => [
new NotBlank([
'message' => 'Introduce un nombre de usuario',
]),
new ValidUsername(),
],
])
->add('email', TextType::class, [
'attr' => [
'class' => 'loginInput'
],
'constraints' => [
new NotBlank([
'message' => 'Introduce una dirección de email válida',
]),
]])
->add('plainPassword', RepeatedType::class, [
'type' => PasswordType::class,
'first_options' => ['label' => 'Nueva Contraseña'],
'second_options' => ['label' => 'Repetir Nueva Contraseña'],
'invalid_message' => 'Las contraseñas no coinciden.',
'constraints' => [
new NotBlank([
'message' => 'Introduce una contraseña',
]),
new Length([
'min' => 6,
'minMessage' => 'Tu contraseña debe tener al menos {{ limit }} caracteres',
// max length allowed by Symfony for security reasons
'max' => 4096,
]),
],
'mapped' => false,
'attr' => ['autocomplete' => 'new-password', 'class' => 'loginInput'],
])
;
}
public function configureOptions(OptionsResolver $resolver): void {
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}