ConstraintValidatorFactory example

$loader = null;

            if (\count($loaders) > 1) {
                $loader = new LoaderChain($loaders);
            } elseif (1 === \count($loaders)) {
                $loader = $loaders[0];
            }

            $metadataFactory = new LazyLoadingMetadataFactory($loader$this->mappingCache);
        }

        $validatorFactory = $this->validatorFactory ?? new ConstraintValidatorFactory();
        $translator = $this->translator;

        if (null === $translator) {
            $translator = new class() implements TranslatorInterface, LocaleAwareInterface {
                use TranslatorTrait;
            };
            // Force the locale to be 'en' when no translator is provided rather than relying on the Intl default locale             // This avoids depending on Intl or the stub implementation being available. It also ensures that Symfony             // validation messages are pluralized properly even when the default locale gets changed because they are in             // English.             $translator->setLocale('en');
        }
    $container = new ContainerBuilder();
    $container->set('typed_data_manager', $this->typedDataManager);
    \Drupal::setContainer($container);

    $translator = $this->createMock('Drupal\Core\Validation\TranslatorInterface');
    $translator->expects($this->any())
      ->method('trans')
      ->willReturnCallback(function D$id) {
        return $id;
      });
    $this->contextFactory = new ExecutionContextFactory($translator);
    $this->validatorFactory = new ConstraintValidatorFactory();
    $this->recursiveValidator = new RecursiveValidator($this->contextFactory, $this->validatorFactory, $this->typedDataManager);
  }

  /** * Ensures that passing an explicit group is not supported. * * @covers ::validate */
  public function testValidateWithGroups() {
    $this->expectException(\LogicException::class);
    $this->recursiveValidator->validate('test', NULL, 'test group');
  }
namespace Symfony\Component\Validator\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\ConstraintValidatorFactory;
use Symfony\Component\Validator\Tests\Fixtures\DummyConstraint;
use Symfony\Component\Validator\Tests\Fixtures\DummyConstraintValidator;

class ConstraintValidatorFactoryTest extends TestCase
{
    public function testGetInstance()
    {
        $factory = new ConstraintValidatorFactory();
        $this->assertInstanceOf(DummyConstraintValidator::class$factory->getInstance(new DummyConstraint()));
    }

    public function testPredefinedGetInstance()
    {
        $validator = new DummyConstraintValidator();
        $factory = new ConstraintValidatorFactory([DummyConstraintValidator::class => $validator]);
        $this->assertSame($validator$factory->getInstance(new DummyConstraint()));
    }
}
$violations = $this->validator->validate($entity, null, ['Default', 'group1', 'group2']);

        $this->assertCount(3, $violations);
    }

    protected function createValidator(MetadataFactoryInterface $metadataFactory, array $objectInitializers = []): ValidatorInterface
    {
        $translator = new IdentityTranslator();
        $translator->setLocale('en');

        $contextFactory = new ExecutionContextFactory($translator);
        $validatorFactory = new ConstraintValidatorFactory();

        return new RecursiveValidator($contextFactory$metadataFactory$validatorFactory$objectInitializers);
    }

    public function testEmptyGroupsArrayDoesNotTriggerDeprecation()
    {
        $entity = new Entity();
        $childA = new ChildA();
        $childB = new ChildB();
        $childA->name = false;
        $childB->name = 'fake';
        
public function setValidator(ValidatorInterface $validator) {
    $this->validator = $validator;
  }

  /** * {@inheritdoc} */
  public function getValidator() {
    if (!isset($this->validator)) {
      $this->validator = new RecursiveValidator(
        new ExecutionContextFactory(new DrupalTranslator()),
        new ConstraintValidatorFactory($this->classResolver),
        $this
      );
    }
    return $this->validator;
  }

  /** * {@inheritdoc} */
  public function setValidationConstraintManager(ConstraintManager $constraintManager) {
    $this->constraintManager = $constraintManager;
  }
$loader = null;

            if (\count($loaders) > 1) {
                $loader = new LoaderChain($loaders);
            } elseif (1 === \count($loaders)) {
                $loader = $loaders[0];
            }

            $metadataFactory = new LazyLoadingMetadataFactory($loader$this->mappingCache);
        }

        $validatorFactory = $this->validatorFactory ?? new ConstraintValidatorFactory();
        $translator = $this->translator;

        if (null === $translator) {
            $translator = new class() implements TranslatorInterface, LocaleAwareInterface {
                use TranslatorTrait;
            };
            // Force the locale to be 'en' when no translator is provided rather than relying on the Intl default locale             // This avoids depending on Intl or the stub implementation being available. It also ensures that Symfony             // validation messages are pluralized properly even when the default locale gets changed because they are in             // English.             $translator->setLocale('en');
        }
Home | Imprint | This part of the site doesn't use cookies.