/** * {@inheritdoc} */ public static function loadValidatorMetadata(ClassMetadata $classMetadata) { $classMetadata->addPropertyConstraint('title', Assert::stringType()->notBlank()); $classMetadata->addPropertyConstraint('content', Assert::stringType()); $classMetadata->addPropertyConstraint('title', Assert::intType()->notBlank(), 'foo'); $classMetadata->addPropertyConstraint('content', Assert::intType()->notBlank(), 'foo'); }
/** * {@inheritdoc} */ public static function loadValidatorMetadata(ClassMetadata $classMetadata) { $classMetadata->addPropertyConstraint('email', Assert::email()); $classMetadata->addPropertyConstraint('password', Assert::stringType()->notBlank()); }
/** * @param object $object */ protected function addObjectConstraints($object) { $metadata = $this->metadataFactory->getMetadataForClass(get_class($object)); if ($metadata !== null) { /** @var ClassMetadata $classMetadata */ $classMetadata = $metadata->getRootClassMetadata(); foreach ($classMetadata->getPropertiesMetadata() as $propertyMetadata) { foreach ($propertyMetadata->getConstraints() as $group => $constraints) { $allOf = Assert::create(); foreach ($constraints as $constraint) { $allOf->addRules($constraint->getRules()); } $this->addConstraint(Assert::create()->attribute($propertyMetadata->getPropertyName(), $allOf), get_class($object), $group); } } } }
/** * Test validate method. */ public function testValidateExplicitConstraints() { $this->given($validator = $this->createValidator())->then()->boolean($validator->validate('ivannis', Assert::alnum()->noWhitespace()->length(1, 15)))->isTrue()->boolean($validator->validate('ivannis', Assert::alnum()->noWhitespace()->length(1, 15), 'foo'))->isTrue()->boolean($validator->validate('some tests', Assert::alnum()->noWhitespace()->length(1, 15)))->isFalse()->exception(function () use($validator) { $validator->validate('value'); })->isInstanceOf(\RuntimeException::class); }
/** * {@inheritdoc} */ public static function loadValidatorMetadata(ClassMetadata $classMetadata) { $classMetadata->addPropertyConstraint('email', Assert::email()); }
/** * Test create method. */ public function testCreate() { $this->given($validator = Assert::create())->then()->object($validator)->isInstanceOf(Assert::class); }