<?php declare(strict_types=1);
/**
* @Description: validation dynamique depot annonce partenaire : type_partenaire personne_morale ou non
* @author Mehrez Labidi
*/
namespace App\Form\Type\DepotAnnonce;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use App\Validator\Constraints\Annee;
use App\Validator\Constraints\Montant;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\NotBlank;
class ValidatorTypePartenaire implements EventSubscriberInterface {
/**
* @return string[]
*/
public static function getSubscribedEvents(): array {
return [
FormEvents::PRE_SET_DATA => 'onPreSetData',
FormEvents::PRE_SUBMIT => 'onPreSubmit'
];
}
/**
*
* @param FormEvent $event
* @return void
*/
public function onPreSetData(FormEvent $event): void {
$this->validation($event);
}
/**
*
* @param FormEvent $event
* @return void
*/
public function onPreSubmit(FormEvent $event): void {
$this->validation($event);
}
/**
*
* @param type $event
*/
private function validation($event) {
$user = $event->getData();
$form = $event->getForm();
if (!empty($user['type_partenaire'])) {
if ($user['type_partenaire'] === "personne_morale") {
$form->add('nb_personnes_min', TextType::class,
[
"required" => true,
"constraints" => [
new NotBlank(["message" => "Le champ nb personnes min est obligatoire"]),
new Assert\Length(['max' => 100, 'maxMessage' => "Le champ nb personnes min est trop long"])
], "attr" => ["autocomplete" => "off"]
]);
$form->add('nb_personnes_max', TextType::class,
[
"required" => true,
"constraints" => [
new NotBlank(["message" => "Le champ nb personnes max est obligatoire"]),
new Assert\Length(['max' => 100, 'maxMessage' => "Le champ nb personnes max est trop long"])
], "attr" => ["autocomplete" => "off"]
]);
$form->add('ca_min', TextType::class,
[
"required" => true,
"constraints" => [
new NotBlank(["message" => "Le champ ca min est obligatoire"]),
new Assert\Length(['max' => 100, 'maxMessage' => "Le champ ca min est trop long"])
], "attr" => ["autocomplete" => "off"]
]);
$form->add('ca_max', TextType::class,
[
"required" => true,
"constraints" => [
new NotBlank(["message" => "Le champ ca max est obligatoire"]),
new Assert\Length(['max' => 100, 'maxMessage' => "Le champ ca max est trop long"])
], "attr" => ["autocomplete" => "off"]
]);
}
}
}
}