<?php
namespace App\Form;
use App\Entity\User;
use App\Service\TextResolverService;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Contracts\Translation\TranslatorInterface;
class RegistrationType extends AbstractType {
/** @var TranslatorInterface */
private $translator;
/** @var TextResolverService */
private $textResolver;
public function __construct(TranslatorInterface $translator, TextResolverService $textResolver) {
$this->translator = $translator;
$this->textResolver = $textResolver;
}
public function buildForm(FormBuilderInterface $builder, array $options): void {
$builder
->add('email', TextType::class, [
'attr' => [
'required' => true,
],
'disabled' => $options['address_update'],
])
->add('firstname', TextType::class, [
'attr' => [
'required' => true,
],
'disabled' => $options['address_update'],
])
->add('lastname', TextType::class, [
'attr' => [
'required' => true,
],
'disabled' => $options['address_update'],
]);
if (!$options['address_update']) {
$builder->add('plainPassword', PasswordType::class, [
// instead of being set onto the object directly,
// this is read and encoded in the controller
'mapped' => false,
'attr' => ['autocomplete' => 'new-password'],
'constraints' => [
new NotBlank([
'message' => $this->translator->trans('form.field.password.not_blank'),
]),
new Length([
'min' => 6,
'minMessage' => $this->translator->trans('form.field.password.too_short'),
// max length allowed by Symfony for security reasons
'max' => 4096,
]),
],
]);
}
$builder->add('addressBook', AddressBookType::class, [
'required' => false,
]);
$builder->add('agreeToTiInfos', CheckboxType::class, [
'label' => strip_tags($this->textResolver->resolve('register.page.box.newsletter.consent')),
'required' => false,
]);
$submitLabel = 'Registrieren';
if ($options['address_update']) {
$submitLabel = 'Speichern';
}
$builder->add('submit', SubmitType::class, [
'attr' => [
'class' => 'button accent w-100 mt-5',
],
'label' => $submitLabel,
]);
}
public function configureOptions(OptionsResolver $resolver): void {
$resolver->setDefaults([
'data_class' => User::class,
'during_order' => false,
'address_update' => false,
]);
}
}