Skip to content

Instantly share code, notes, and snippets.

@artyuum
Last active February 8, 2021 06:22
Show Gist options
  • Save artyuum/03e2ebf61fbf9d246f5ec08aaf861120 to your computer and use it in GitHub Desktop.
Save artyuum/03e2ebf61fbf9d246f5ec08aaf861120 to your computer and use it in GitHub Desktop.
Converts Validator constrains violations to a simple key => value array (field => error messages)
<?php
class ConstraintViolationHelper
{
/**
* Gets the validator errors.
*/
public static function toArray(ConstraintViolationListInterface $violations): ?array
{
$errors = null;
// loops through all violations and stores only their message
foreach ($violations as $violation) {
$errors[$violation->getPropertyPath()][] = $violation->getMessage();
}
return $errors;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment