InlineValidator.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. $service = $this->container->get($constraint->getService());
  27. $errorElement = new ErrorElement(
  28. $value,
  29. $this->constraintValidatorFactory,
  30. $this->context,
  31. $this->context->getGroup()
  32. );
  33. call_user_func(array($service, $constraint->getMethod()), $errorElement, $value);
  34. return count($this->context->getViolations()) == 0;
  35. }
  36. }