JSONValidator.php 691 B

123456789101112131415161718192021222324252627
  1. <?php
  2. namespace ExtraDataBundle\Validator;
  3. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  4. class JSONValidator
  5. {
  6. /**
  7. * @param string $value
  8. * @param ExecutionContextInterface $context
  9. * @param array $payload
  10. */
  11. public static function validate($value, ExecutionContextInterface $context, $payload)
  12. {
  13. if (isset($payload['field']) && $value) {
  14. json_decode($value);
  15. if (json_last_error() !== JSON_ERROR_NONE) {
  16. $context->buildViolation('error.json_format_invalid')
  17. ->atPath($payload['field'])
  18. ->addViolation();
  19. }
  20. }
  21. }
  22. }