Last active
February 8, 2021 06:22
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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