function onFlush(OnFlushEventArgs $args) { foreach ($this->getEntitiesToValidate($args->getEntityManager()) as $entity) { if (count($violations = $this->validator->validate($entity))) { $message = "Invalid entity {$this->objectToString($entity)}:\n"; $message .= implode("\n", Collection::map($violations, function ($violation) { return "- {$violation->getPropertyPath()}: {$violation->getMessage()}"; })); throw new \InvalidArgumentException($message); } } }