<?php
declare(strict_types = 1);
namespace App\Form\Type;
use App\Domain\DTO\ContactDTO;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
class ContactFormType extends AbstractType
{
public function buildForm(
FormBuilderInterface $builder,
array $options
) {
$path = Request::createFromGlobals();
$path = $path->getPathInfo();
$builder
->add('name', TextType::class, [
'label' => false,
'mapped' => false,
'constraints' => [
new NotBlank([
'message' => 'Merci de remplir ce champ',
]),
]
])
->add('firstname', TextType::class, [
'label' => false,
'mapped' => false,
'constraints' => [
new NotBlank([
'message' => 'Merci de remplir ce champ',
]),
],
'required' => true
])
->add('mail', EmailType::class, [
'label' => false,
'mapped' => false,
'constraints' => [
new Email([
'message' => 'Merci d\'entrer un mail valide',
]),
new NotBlank([
'message' => 'Merci de remplir ce champ',
]),
]
])
->add('phone', TextType::class, [
'label' => false,
'mapped' => false,
'required' => true,
'constraints' => [
new NotBlank([
'message' => 'Merci de remplir ce champ',
]),
]
])
->add('file', FileType::class, [
'label' => false,
'mapped' => false,
'required' => false
])
->add('object', ChoiceType::class, [
'choices' => [
'Je recherche un bien' => 'Je recherche un bien',
'Je vends un bien' => 'Je vends un bien',
'Je souhaite plus d’informations sur un projet' => 'Je souhaite plus d’informations sur un projet',
'Autre' => 'Autre'
],
'label' => false,
'mapped' => false,
'constraints' => [
new NotBlank([
'message' => 'Merci de remplir ce champ',
]),
],
'data' => ($path === '/vendre') ? 'Je vends un bien' : 'Je recherche un bien'
])
->add('message', TextareaType::class, [
'label' => false,
'mapped' => false,
'constraints' => [
new NotBlank([
'message' => 'Merci de remplir ce champ',
]),
]
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => ContactDTO::class,
'empty_data' => function (FormInterface $form) {
return new ContactDTO(
$form->get('name')->getData(),
$form->get('firstname')->getData(),
$form->get('mail')->getData(),
$form->get('phone')->getData(),
$form->get('file')->getData(),
$form->get('object')->getData(),
$form->get('message')->getData()
);
}
]);
}
}