InlineValidator.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. $errorElement = new ErrorElement(
  27. $value,
  28. $this->constraintValidatorFactory,
  29. $this->context,
  30. $this->context->getGroup()
  31. );
  32. if ($constraint->isClosure()) {
  33. $function = $constraint->getClosure();
  34. } else {
  35. if (is_string($constraint->getService())) {
  36. $service = $this->container->get($constraint->getService());
  37. } else {
  38. $service = $constraint->getService();
  39. }
  40. $function = array($service, $constraint->getMethod());
  41. }
  42. call_user_func($function, $errorElement, $value);
  43. return count($this->context->getViolations()) == 0;
  44. }
  45. }