* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Sonata\AdminBundle\Validator; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Constraint; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory; use Sonata\AdminBundle\Validator\ErrorElement; class InlineValidator extends ConstraintValidator { protected $container; /** * @param \Symfony\Component\DependencyInjection\ContainerInterface $container * @param \Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory $constraintValidatorFactory */ public function __construct(ContainerInterface $container, ConstraintValidatorFactory $constraintValidatorFactory) { $this->container = $container; $this->constraintValidatorFactory = $constraintValidatorFactory; } /** * {@inheritDoc} */ public function isValid($value, Constraint $constraint) { $errorElement = new ErrorElement( $value, $this->constraintValidatorFactory, $this->context, $this->context->getGroup() ); if ($constraint->isClosure()) { $function = $constraint->getClosure(); } else { if (is_string($constraint->getService())) { $service = $this->container->get($constraint->getService()); } else { $service = $constraint->getService(); } $function = array($service, $constraint->getMethod()); } call_user_func($function, $errorElement, $value); return count($this->context->getViolations()) == 0; } }