Skip to content

Commit 1cad546

Browse files
committed
Ajout du champ 'année' au formulaire d'événement et mise à jour du modèle pour gérer sa valeur
1 parent cef8511 commit 1cad546

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

sources/AppBundle/Event/Form/EventType.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Symfony\Component\Form\Extension\Core\Type\TextType;
1616
use Symfony\Component\Form\Extension\Core\Type\UrlType;
1717
use Symfony\Component\Form\FormBuilderInterface;
18+
use Symfony\Component\Form\FormEvent;
19+
use Symfony\Component\Form\FormEvents;
1820
use Symfony\Component\OptionsResolver\OptionsResolver;
1921
use Symfony\Component\Validator\Constraints as Assert;
2022

@@ -168,6 +170,13 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
168170
new Assert\File(mimeTypes: 'application/pdf'),
169171
],
170172
])
173+
->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event): void {
174+
/** @var Event $event */
175+
$event = $event->getData();
176+
if ($event->getDateStart() !== null) {
177+
$event->setYear(date('Y', $event->getDateStart()->getTimestamp()));
178+
}
179+
})
171180
;
172181
}
173182

sources/AppBundle/Event/Model/Event.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class Event implements NotifyPropertyInterface
2727

2828
private ?DateTime $dateEnd = null;
2929

30+
private ?string $year = null;
31+
3032
/**
3133
* @var DateTime
3234
*/
@@ -187,6 +189,18 @@ public function setDateEnd(?DateTime $dateEnd): self
187189
return $this;
188190
}
189191

192+
public function getYear(): ?string
193+
{
194+
return $this->year;
195+
}
196+
197+
public function setYear(?string $year): self
198+
{
199+
$this->propertyChanged('year', $this->year, $year);
200+
$this->year = $year;
201+
return $this;
202+
}
203+
190204
/**
191205
* @return DateTime
192206
*/

sources/AppBundle/Event/Model/Repository/EventRepository.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ public static function initMetadata(SerializerFactoryInterface $serializerFactor
323323
'unserialize' => ['unSerializeUseFormat' => false],
324324
],
325325
])
326+
->addField([
327+
'columnName' => 'annee',
328+
'fieldName' => 'year',
329+
'type' => 'string',
330+
])
326331
->addField([
327332
'columnName' => 'date_fin_appel_projet',
328333
'fieldName' => 'dateEndCallForProjects',

0 commit comments

Comments
 (0)