src/EventSubscriber/LeaveRequestRequestGuardSubscriber.php line 31
<?phpnamespace App\EventSubscriber;use App\Entity\LeaveRequest;use App\Validator\NoLeaveOverlap;use App\Validator\LeaveDurationNotZero;use App\Validator\WorkMonthNotValidated;use Symfony\Component\Workflow\Event\GuardEvent;use App\Validator\LeaveWithDurationWithinContractPeriod;use Symfony\Component\Validator\Validator\ValidatorInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;class LeaveRequestRequestGuardSubscriber implements EventSubscriberInterface{public function __construct(private readonly ValidatorInterface $validator) {}public static function getSubscribedEvents(): array{$workflowName = 'leave_request';$transitionName = 'request';return ['workflow.' . $workflowName . '.guard.' . $transitionName => ['guardRequest'],];}public function guardRequest(GuardEvent $event): void{$subject = $event->getSubject();if (!$subject instanceof LeaveRequest) {return;}// Need to re-test the validation constraints that might// have been unvalidated (outside the edit form) since drafting$violations = $this->validator->validate($subject, [new NoLeaveOverlap(),new LeaveWithDurationWithinContractPeriod(),new WorkMonthNotValidated(),new LeaveDurationNotZero(),]);$violationsCount = $violations->count();if ($violationsCount > 0) {$message = '';for ($violationIndex = 0; $violationIndex < $violationsCount; $violationIndex++) {$message .= $violations->get($violationIndex)->getMessage() . ' ';}$event->setBlocked(true,$message);}}}