<?php
namespace App\Form;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class RegistrationFormType extends AbstractType
{
private $translator;
private $requestStack;
private $session;
public function __construct(TranslatorInterface $translator,RequestStack $requestStack)
{
$this->setTranslator($translator);
$this->requestStack = $requestStack;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$this->session = $this->getRequestStack()->getSession();
$locale = $this->getSession()->get('_locale', 'en');
$emaillable= $this->getTranslator()->trans('email'. $locale);
$pseudolable= $this->getTranslator()->trans('pseudo'. $locale);
$agreelable= $this->getTranslator()->trans('agreeterms'. $locale);
$agreemsglable= $this->getTranslator()->trans('agreetermsmsg'. $locale);
$acceptCgvlable= $this->getTranslator()->trans('acceptCgv'. $locale);
$acceptpromolable= $this->getTranslator()->trans('acceptPubPromo'. $locale);
$passlabel= $this->getTranslator()->trans('password'. $locale);
$passreplabel= $this->getTranslator()->trans('passwordrep'. $locale);
$builder
->add('email',EmailType::class, ['label' => $emaillable])
->add('pseudo',TextType::class, ['label' => $pseudolable])
->add('agreeTerms', CheckboxType::class, [
'label' => $agreelable,
'mapped' => false,
'constraints' => [
new IsTrue([
'message' => $agreemsglable,
]),
],
])
->add('acceptCgv', CheckboxType::class, [
'label' => $acceptCgvlable,
'mapped' => true,
'constraints' => [
new IsTrue([
'message' => 'You should agree to our terms.',
]),
],
])
->add('acceptPublicationPromotion', CheckboxType::class, [
'label' => $acceptpromolable,
'mapped' => true,
'required' => false
])
->add('plainPassword', RepeatedType::class, [
'label' => $passlabel,
'mapped' => false,
'type' => PasswordType::class,
'invalid_message' => 'The password fields must match.',
'options' => ['attr' => ['class' => 'password-field']],
'required' => true,
'first_options' => ['label' => $passlabel],
'second_options' => ['label' => $passreplabel],
]);
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
/**
* Get the value of translator
*/
public function getTranslator()
{
return $this->translator;
}
/**
* Set the value of translator
*
* @return self
*/
public function setTranslator($translator)
{
$this->translator = $translator;
return $this;
}
/**
* Get the value of requestStack
*/
public function getRequestStack()
{
return $this->requestStack;
}
/**
* Get the value of session
*/
public function getSession()
{
return $this->session;
}
}