InlineValidator.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Validator;
  11. use Symfony\Component\Validator\ConstraintValidator;
  12. use Symfony\Component\Validator\Constraint;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory;
  15. use Sonata\AdminBundle\Validator\ErrorElement;
  16. class InlineValidator extends ConstraintValidator
  17. {
  18. protected $container;
  19. public function __construct(ContainerInterface $container, ConstraintValidatorFactory $constraintValidatorFactory)
  20. {
  21. $this->container = $container;
  22. $this->constraintValidatorFactory = $constraintValidatorFactory;
  23. }
  24. public function isValid($value, Constraint $constraint)
  25. {
  26. if (is_string($constraint->getService())) {
  27. $service = $this->container->get($constraint->getService());
  28. } else {
  29. $service = $constraint->getService();
  30. }
  31. $errorElement = new ErrorElement(
  32. $value,
  33. $this->constraintValidatorFactory,
  34. $this->context,
  35. $this->context->getGroup()
  36. );
  37. call_user_func(array($service, $constraint->getMethod()), $errorElement, $value);
  38. return count($this->context->getViolations()) == 0;
  39. }
  40. }