<?php
namespace App\Form;
use App\Entity\Client;
use App\Form\UserType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
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\Length;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\NotBlank;
use App\Validator\Constraints\ValidPhoneNumber;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class ClientType extends AbstractType
{
private $router;
private $translator;
public function __construct(RouterInterface $router, TranslatorInterface $translator)
{
$this->router = $router;
$this->translator = $translator;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$termsUrl = $this->router->generate('app_front_terms');
$privacyUrl = $this->router->generate('app_front_privacy');
$builder
->add('user', UserType::class, [
'label' => true,
])
->add('entrepriseName', TextType::class, [
'label' => 'Nom de l\'organisme ',
'attr' => [
'placeholder' => 'Nom de l\'entreprise ou l\'association'
],
'constraints' => [
new Length([
'min' => 3,
'minMessage' => 'Le nom de l\'entreprise doit avoir au moins {{ limit }} caractères.',
]),
],
'required' => false
])
->add('siret', TextType::class, [
'label' => 'Numéro Siret ou Siren',
'attr' => [
'maxlength' => '14'
],
'constraints' => [
new Length([
'min' => 9,
'max' => 14,
'minMessage' => 'Le Numéro Siret ou Siren doit avoir au moins {{ limit }} caractères.',
'maxMessage' => 'Le numéro SIRET ou SIREN ne peut pas contenir plus de {{ limit }} caractères.',
]),
],
'required' => false
])
->add('mobilePhone', TelType::class, [
'label' => 'Téléphone mobile',
'attr' => [
'maxlength' => 20
],
'constraints' => [
new Length([
'min' => 10,
'minMessage' => 'Le numéro de téléphone doit avoir au moins {{ limit }} caractères.',
]),
new NotBlank([
'message' => 'Veuillez entrer votre numéro de téléphone !',
]),
new ValidPhoneNumber()
],
'required' => true
])
->add('agreeTerms', CheckboxType::class, [
'mapped' => false,
'constraints' => [
new IsTrue([
'message' => 'Vous devez accepter notre conditions.',
]),
],
'label_attr' => [
'style' => 'width: auto; margin-left: 0.5rem; cursor: pointer !important'
],
'label' => sprintf(
'J\'accepte les <a href="%s" class="text-theme-blue">%s</a> et la <a href="%s" class="text-theme-blue">%s</a>.',
$termsUrl,
$this->translator->trans('conditions d\'utilisation'),
$privacyUrl,
$this->translator->trans('politique de confidentialité')
),
'label_html' => true,
'required' => true
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Client::class,
]);
}
public function getBlockPrefix()
{
return 'sg_client';
}
}