<?php
namespace App\Form\Type;
use App\Entity\PrivateUser;
use App\Parameter\ProviderInterface;
use App\Twig\Extension\CountryExtension;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
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\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
class PrivateRegistrationStep2FormType extends AbstractType
{
public function __construct(private readonly CountryExtension $countryExtension, private readonly ProviderInterface $provider)
{
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$preferredCountry = 'FR';
$visitorCountry = $this->countryExtension->getVisitorCountry();
if ('' !== $visitorCountry) {
$preferredCountry = strtoupper($visitorCountry);
}
$choices[''] = '';
foreach (explode(';', (string) $this->provider->getParameterValue('private.how_did_you_know')) as $value) {
$choices[$value] = $value;
}
$builder
->add('lastname', TextType::class, [
'label' => 'frontend.profile.form.type.label.lastname',
'constraints' => [
new NotBlank(),
],
])
->add('firstname', TextType::class, [
'label' => 'frontend.profile.form.type.label.firstname',
'constraints' => [
new NotBlank(),
],
])
->add('email', EmailType::class, [
'label' => 'frontend.profile.email',
'constraints' => [
new NotBlank(),
new Email(),
],
])
->add('plainPassword', PasswordType::class, [
'required' => true,
'label' => 'frontend.profile.password',
'attr' => ['placeholder' => 'frontend.page.register_pro.form.label.password'],
'constraints' => [
new Length([
'min' => 8,
'max' => 1024,
'minMessage' => 'fos_user.password.short',
]),
new NotBlank(),
],
])
->add('country', CountryType::class, [
'label' => 'frontend.address.form.type.label.country',
'placeholder' => '',
'preferred_choices' => [$preferredCountry],
'data' => $preferredCountry,
'constraints' => [
new NotBlank(),
],
])
->add('referralSource', ChoiceType::class, [
'required' => false,
'label' => 'frontend.address.form.type.label.referralSource',
'choices' => $choices,
'placeholder' => false,
])
->add('newsletter', CheckboxType::class, [
'required' => false,
'label' => 'frontend.registration.stay.informed',
])
->add('recaptcha', EWZRecaptchaCustomType::class)
->add('submit', SubmitType::class, [
'label' => 'Valider', // 'Valider' is a translation key
'attr' => [
'class' => 'btn06',
],
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'locale' => null,
'data_class' => PrivateUser::class,
'validation_groups' => [
'Default',
'Registration',
],
]);
}
}