public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('title', new NotBlank()); $metadata->addPropertyConstraint('coment', new NotBlank()); $metadata->addPropertyConstraint('author', new NotBlank()); $metadata->addPropertyConstraint('photo', new NotBlank()); }
/** * {@inheritDoc} */ public function loadClassMetadata(ClassMetadata $metadata) { $annotClass = 'Symfony\\Component\\Validator\\Constraints\\Validation'; $reflClass = $metadata->getReflectionClass(); $loaded = false; if ($annot = $this->reader->getClassAnnotation($reflClass, $annotClass)) { foreach ($annot->constraints as $constraint) { $metadata->addConstraint($constraint); } $loaded = true; } foreach ($reflClass->getProperties() as $property) { if ($annot = $this->reader->getPropertyAnnotation($property, $annotClass)) { foreach ($annot->constraints as $constraint) { $metadata->addPropertyConstraint($property->getName(), $constraint); } $loaded = true; } } foreach ($reflClass->getMethods() as $method) { if ($annot = $this->reader->getMethodAnnotation($method, $annotClass)) { foreach ($annot->constraints as $constraint) { // TODO: clean this up $name = lcfirst(substr($method->getName(), 0, 3) == 'get' ? substr($method->getName(), 3) : substr($method->getName(), 2)); $metadata->addGetterConstraint($name, $constraint); } $loaded = true; } } return $loaded; }
/** * This is a functinoal test as there is a large integration necessary to get the validator working. */ public function testValidateUniqueness() { $entityManagerName = "foo"; $em = $this->createTestEntityManager(); $schemaTool = new SchemaTool($em); $schemaTool->createSchema(array($em->getClassMetadata('Symfony\\Tests\\Bridge\\Doctrine\\Form\\Fixtures\\SingleIdentEntity'))); $entity1 = new SingleIdentEntity(1, 'Foo'); $registry = $this->createRegistryMock($entityManagerName, $em); $uniqueValidator = new UniqueEntityValidator($registry); $metadata = new ClassMetadata('Symfony\\Tests\\Bridge\\Doctrine\\Form\\Fixtures\\SingleIdentEntity'); $metadata->addConstraint(new UniqueEntity(array('fields' => array('name'), 'em' => $entityManagerName))); $metadataFactory = $this->createMetadataFactoryMock($metadata); $validatorFactory = $this->createValidatorFactory($uniqueValidator); $validator = new Validator($metadataFactory, $validatorFactory); $violationsList = $validator->validate($entity1); $this->assertEquals(0, $violationsList->count(), "No violations found on entity before it is saved to the database."); $em->persist($entity1); $em->flush(); $violationsList = $validator->validate($entity1); $this->assertEquals(0, $violationsList->count(), "No violations found on entity after it was saved to the database."); $entity2 = new SingleIdentEntity(2, 'Foo'); $violationsList = $validator->validate($entity2); $this->assertEquals(1, $violationsList->count(), "No violations found on entity after it was saved to the database."); $violation = $violationsList[0]; $this->assertEquals('This value is already used.', $violation->getMessage()); $this->assertEquals('name', $violation->getPropertyPath()); $this->assertEquals('Foo', $violation->getInvalidValue()); }
/** * Load validator metadata * * @param Symfony\Component\Validator\Mapping\ClassMetadata $metadata */ public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('created', new Assert\NotBlank()); $metadata->addPropertyConstraint('title', new Assert\NotBlank()); $metadata->addPropertyConstraint('title', new Assert\Length(array('min' => 5))); $metadata->addPropertyConstraint('body', new Assert\NotBlank()); }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('teamA', new Assert\Valid()); $metadata->addPropertyConstraint('teamB', new Assert\Valid()); $metadata->addPropertyConstraint('setScores', new Assert\Valid()); $metadata->addPropertyConstraint('gameTimeLengthInSeconds', new Assert\Range(['min' => 0, 'max' => 65535])); }
/** * @param ClassMetadata $metadata */ public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('title', new NotBlank(array('message' => 'You must enter a title'))); $metadata->addPropertyConstraint('blog', new NotBlank(array('message' => 'You must enter little bit blog?'))); $metadata->addPropertyConstraint('author', new NotBlank(array('message' => 'You must enter your name!'))); $metadata->addPropertyConstraint('tags', new NotBlank(array('message' => 'You must enter minimal one tag'))); }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('name', new NotBlank()); $metadata->addPropertyConstraint('email', new Email()); $metadata->addPropertyConstraint('short_name', new NotBlank()); $metadata->addPropertyConstraint('address', new NotBlank()); }
/** * Test MetaData merge with parent annotation. */ public function testLoadClassMetadataAndMerge() { $loader = new AnnotationLoader(); // Load Parent MetaData $parent_metadata = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\EntityParent'); $loader->loadClassMetadata($parent_metadata); $metadata = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\Entity'); // Merge parent metaData. $metadata->mergeConstraints($parent_metadata); $loader->loadClassMetadata($metadata); $expected_parent = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\EntityParent'); $expected_parent->addPropertyConstraint('other', new NotNull()); $expected_parent->getReflectionClass(); $expected = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\Entity'); $expected->mergeConstraints($expected_parent); $expected->setGroupSequence(array('Foo', 'Entity')); $expected->addConstraint(new NotNull()); $expected->addConstraint(new ConstraintA()); $expected->addConstraint(new Min(3)); $expected->addConstraint(new Choice(array('A', 'B'))); $expected->addConstraint(new All(array(new NotNull(), new Min(3)))); $expected->addConstraint(new All(array('constraints' => array(new NotNull(), new Min(3))))); $expected->addConstraint(new Collection(array('fields' => array('foo' => array(new NotNull(), new Min(3)), 'bar' => new Min(5))))); $expected->addPropertyConstraint('firstName', new Choice(array('message' => 'Must be one of %choices%', 'choices' => array('A', 'B')))); $expected->addGetterConstraint('lastName', new NotNull()); // load reflection class so that the comparison passes $expected->getReflectionClass(); $this->assertEquals($expected, $metadata); }
/** * Valorizes model validation metadata * * @return void */ public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addGetterConstraint('title', new Assert\NotBlank()); $metadata->addGetterConstraint('content', new Assert\NotBlank()); $metadata->addGetterConstraint('image', new Assert\NotBlank()); $metadata->addGetterConstraint('image', new Assert\Image(['maxWidth' => 400])); }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('name', new NotBlank()); $metadata->addPropertyConstraint('name', new Length(['min' => 5, 'max' => 10, 'minMessage' => 'Your name must be at least {{ limit }} characters long', 'maxMessage' => 'Your name cannot be longer than {{ limit }} characters'])); $metadata->addPropertyConstraint('password', new NotBlank()); $metadata->addPropertyConstraint('password', new Length(['min' => 5, 'max' => 10, 'minMessage' => 'Your password must be at least {{ limit }} characters long', 'maxMessage' => 'Your password cannot be longer than {{ limit }} characters'])); }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('name', new NotBlank()); $metadata->addPropertyConstraint('email', new NotBlank()); $metadata->addPropertyConstraint('email', new Email(array('message' => 'Please provide a valid email address.'))); $metadata->addPropertyConstraint('subject', new NotBlank()); }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('name', new Constraints\NotBlank()); $metadata->addPropertyConstraint('name', new Constraints\MinLength(2)); $metadata->addPropertyConstraint('username', new Constraints\NotBlank()); $metadata->addPropertyConstraint('username', new Constraints\MinLength(2)); }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('login', new NotBlank()); $metadata->addPropertyConstraint('login', new Length(array('min' => 6, 'max' => 40))); $metadata->addPropertyConstraint('email', new Length(array('min' => 2, 'max' => 100))); $metadata->addPropertyConstraint('email', new Email()); }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('title', new NotBlank(array('message' => 'You must enter a title'))); $metadata->addPropertyConstraint('author', new NotBlank(array('message' => 'You must enter a author'))); $metadata->addPropertyConstraint('blog', new NotBlank(array('message' => 'You must enter the text'))); $metadata->addPropertyConstraint('blog', new Length(array('min' => 50))); }
public function loadClassMetadata(ClassMetadata $metadata) { /** @var \ReflectionClass $refClass */ $refClass = $metadata->getReflectionClass(); $baseClass = $this->classNameProvider->getBaseClass($refClass->name); if (!$baseClass) { return false; } if (!array_key_exists($baseClass, $this->config->models)) { return false; } foreach ($this->config->models[$baseClass]->properties as $propName => $property) { if ($property->validation) { if (!is_array($property->validation)) { throw new \LogicException("Configuration error: {$baseClass}:{$propName}:validation must be array"); } foreach ($property->validation as $index => $rule) { if (!is_array($rule) or count($rule) != 1) { throw new \LogicException("Configuration error: {$baseClass}:{$propName}:validation: rule #{$index} must be array of 1 element which contains array"); } $constraintClass = array_keys($rule)[0]; $constraintOptions = $rule[$constraintClass]; $fullClass = $this->getFullConstraintClass($constraintClass); $metadata->addGetterConstraint($propName, new $fullClass($constraintOptions)); } } } return true; }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('column', new Assert\NotBlank()); $metadata->addPropertyConstraint('column', new Assert\Type(['type' => 'integer'])); $metadata->addPropertyConstraint('dir', new Assert\NotBlank()); $metadata->addPropertyConstraint('dir', new Assert\Choice(['choices' => ['asc', 'desc']])); }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('name', new Assert\NotBlank()); $metadata->addPropertyConstraint('name', new Assert\Length(['max' => 32])); $metadata->addPropertyConstraint('description', new Assert\NotBlank()); $metadata->addPropertyConstraint('description', new Assert\Length(['max' => 255])); }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('name', new Assert\NotBlank()); $metadata->addPropertyConstraint('name', new Assert\Length(['max' => 255])); $metadata->addPropertyConstraint('sortOrder', new Assert\NotNull()); $metadata->addPropertyConstraint('sortOrder', new Assert\Range(['min' => 0, 'max' => 65535])); }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('name', new NotBlank()); $metadata->addPropertyConstraint('email', new Email()); $metadata->addPropertyConstraint('subject', new NotBlank()); //$metadata->addPropertyConstraint('subject', new MaxLength(50)); // $metadata->addPropertyConstraint('body', new MinLength(50)); }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $integerColumns = ['origSubtotal', 'subtotal', 'taxSubtotal', 'discount', 'shipping', 'shippingDiscount', 'tax', 'total', 'savings']; foreach ($integerColumns as $columnName) { $metadata->addPropertyConstraint($columnName, new Assert\NotNull()); $metadata->addPropertyConstraint($columnName, new Assert\Range(['min' => 0, 'max' => 4294967295])); } }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('name', new Assert\Length(['max' => 255])); $metadata->addPropertyConstraint('type', new Assert\Valid()); $metadata->addPropertyConstraint('value', new Assert\Range(['min' => 0, 'max' => 4294967295])); self::loadPromotionRedemptionValidatorMetadata($metadata); self::loadPromotionStartEndDateValidatorMetadata($metadata); }
/** * Add validation rules * * @param ClassMetadata $metadata */ public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('ref', new Assert\Blank()); $metadata->addPropertyConstraint('alert', new Assert\NotBlank()); $metadata->addPropertyConstraint('alert', new Assert\Choice(array('callback' => array('\\WSColissimo\\WSColiPosteLetterService\\Request\\ValueObject\\Choice\\AlertType', 'getChoices')))); $metadata->addPropertyConstraint('addressVO', new Assert\NotBlank()); $metadata->addPropertyConstraint('addressVO', new Assert\Valid()); }
/** * @dataProvider guessRequiredProvider */ public function testGuessRequired($constraint, $guess) { // add distracting constraint $this->metadata->addPropertyConstraint(self::TEST_PROPERTY, new Email()); // add constraint under test $this->metadata->addPropertyConstraint(self::TEST_PROPERTY, $constraint); $this->assertEquals($guess, $this->guesser->guessRequired(self::TEST_CLASS, self::TEST_PROPERTY)); }
public function testLoadGroupSequenceProvider() { $loader = new XmlFileLoader(__DIR__ . '/constraint-mapping.xml'); $metadata = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\GroupSequenceProviderEntity'); $loader->loadClassMetadata($metadata); $expected = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\GroupSequenceProviderEntity'); $expected->setGroupSequenceProvider(true); $this->assertEquals($expected, $metadata); }
/** * Validation for the properties of this Entity * * @static * @param \Symfony\Component\Validator\Mapping\ClassMetadata $metadata */ public static function loadValidatorMetadata(ClassMetadata $metadata) { // Name should never be NULL or a blank string $metadata->addPropertyConstraint('name', new Assert\NotNull()); $metadata->addPropertyConstraint('name', new Assert\NotBlank()); // Targetid may not be NULL or a blank string $metadata->addPropertyConstraint('targetid', new Assert\NotNull()); $metadata->addPropertyConstraint('targetid', new Assert\NotBlank()); }
public function loadClassMetadata(ClassMetadata $metadata) { $class_name = $metadata->getReflectionClass()->getName(); if ($this->loader->hasAdminClass($class_name)) { $admin = $this->loader->getAdminByClass($class_name); $admin->loadValidatorMetadata($metadata); return true; } }
public function testValidatePropertyValue() { $entity = new Entity(); $metadata = new ClassMetadata(get_class($entity)); $metadata->addPropertyConstraint('firstName', new FailingConstraint()); $this->factory->addClassMetadata($metadata); $result = $this->validator->validatePropertyValue(get_class($entity), 'firstName', 'Bernhard'); $this->assertEquals(1, count($result)); }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $callbackConstraintOptions = array('groups' => 'flow_revalidatePreviousSteps_step1'); if (Kernel::VERSION_ID < 20400) { $callbackConstraintOptions['methods'] = array('isDataValid'); } else { $callbackConstraintOptions['callback'] = 'isDataValid'; } $metadata->addConstraint(new Assert\Callback($callbackConstraintOptions)); }
public function createValidator($entityManagerName, $em) { $registry = $this->createRegistryMock($entityManagerName, $em); $uniqueValidator = new UniqueEntityValidator($registry); $metadata = new ClassMetadata('Symfony\\Tests\\Bridge\\Doctrine\\Form\\Fixtures\\SingleIdentEntity'); $metadata->addConstraint(new UniqueEntity(array('fields' => array('name'), 'em' => $entityManagerName))); $metadataFactory = $this->createMetadataFactoryMock($metadata); $validatorFactory = $this->createValidatorFactory($uniqueValidator); return new Validator($metadataFactory, $validatorFactory); }
public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('name', new NotBlank()); $metadata->addPropertyConstraint('email', new Email(array('message' => 'Veuillez saisir une adresse email valide!'))); // $metadata->addPropertyConstraint('subject', new NotBlank()); // $metadata->addPropertyConstraint('subject', new Assert\Length(array( // 'max' => 50, // 'maxMessage' => 'Your subject cannot be longer than {{ limit }} characters long', // ))); $metadata->addPropertyConstraint('body', new Assert\Length(array('min' => 2, 'minMessage' => 'Your message must be at least {{ limit }} characters long'))); }