Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0fc8906

Browse filesBrowse files
committed
[Validator] forced all validation annotations to be in the validation namespace to avoid collisions, removed the need for the wrapping @Validation annotation
Before: /** * @Validation({@datetime()}) */ After: /** * @Validation:DateTime() */ The @Validation:Validation() construct is not needed anymore (it is still supported as this is useful when you have several annotations with the same class). So, the above is equivalent to: /** * @Validation:Validation({@Validation:DateTime()}) */
1 parent 3a4d9cb commit 0fc8906
Copy full SHA for 0fc8906

File tree

Expand file treeCollapse file tree

2 files changed

+38
-29
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+38
-29
lines changed

‎src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/Mapping/Loader/AnnotationLoader.php
+24-11Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\Validator\Exception\MappingException;
1515
use Symfony\Component\Validator\Mapping\ClassMetadata;
1616
use Doctrine\Common\Annotations\AnnotationReader;
17+
use Symfony\Component\Validator\Constraints\Validation;
18+
use Symfony\Component\Validator\Constraint;
1719

1820
class AnnotationLoader implements LoaderInterface
1921
{
@@ -22,30 +24,37 @@ class AnnotationLoader implements LoaderInterface
2224
public function __construct()
2325
{
2426
$this->reader = new AnnotationReader();
25-
$this->reader->setDefaultAnnotationNamespace('Symfony\Component\Validator\Constraints\\');
2627
$this->reader->setAutoloadAnnotations(true);
28+
$this->reader->setAnnotationNamespaceAlias('Symfony\Component\Validator\Constraints\\', 'validation');
2729
}
2830

2931
/**
3032
* {@inheritDoc}
3133
*/
3234
public function loadClassMetadata(ClassMetadata $metadata)
3335
{
34-
$annotClass = 'Symfony\Component\Validator\Constraints\Validation';
3536
$reflClass = $metadata->getReflectionClass();
3637
$loaded = false;
3738

38-
if ($annot = $this->reader->getClassAnnotation($reflClass, $annotClass)) {
39-
foreach ($annot->constraints as $constraint) {
39+
foreach ($this->reader->getClassAnnotations($reflClass) as $constraint) {
40+
if ($constraint instanceof Validation) {
41+
foreach ($constraint->constraints as $constraint) {
42+
$metadata->addConstraint($constraint);
43+
}
44+
} elseif ($constraint instanceof Constraint) {
4045
$metadata->addConstraint($constraint);
4146
}
4247

4348
$loaded = true;
4449
}
4550

4651
foreach ($reflClass->getProperties() as $property) {
47-
if ($annot = $this->reader->getPropertyAnnotation($property, $annotClass)) {
48-
foreach ($annot->constraints as $constraint) {
52+
foreach ($this->reader->getPropertyAnnotations($property) as $constraint) {
53+
if ($constraint instanceof Validation) {
54+
foreach ($constraint->constraints as $constraint) {
55+
$metadata->addPropertyConstraint($property->getName(), $constraint);
56+
}
57+
} elseif ($constraint instanceof Constraint) {
4958
$metadata->addPropertyConstraint($property->getName(), $constraint);
5059
}
5160

@@ -54,11 +63,15 @@ public function loadClassMetadata(ClassMetadata $metadata)
5463
}
5564

5665
foreach ($reflClass->getMethods() as $method) {
57-
if ($annot = $this->reader->getMethodAnnotation($method, $annotClass)) {
58-
foreach ($annot->constraints as $constraint) {
59-
// TODO: clean this up
60-
$name = lcfirst(substr($method->getName(), 0, 3)=='get' ? substr($method->getName(), 3) : substr($method->getName(), 2));
66+
foreach ($this->reader->getMethodAnnotations($method) as $constraint) {
67+
// TODO: clean this up
68+
$name = lcfirst(substr($method->getName(), 0, 3)=='get' ? substr($method->getName(), 3) : substr($method->getName(), 2));
6169

70+
if ($constraint instanceof Validation) {
71+
foreach ($constraint->constraints as $constraint) {
72+
$metadata->addGetterConstraint($name, $constraint);
73+
}
74+
} elseif ($constraint instanceof Constraint) {
6275
$metadata->addGetterConstraint($name, $constraint);
6376
}
6477

@@ -68,4 +81,4 @@ public function loadClassMetadata(ClassMetadata $metadata)
6881

6982
return $loaded;
7083
}
71-
}
84+
}

‎tests/Symfony/Tests/Component/Validator/Fixtures/Entity.php

Copy file name to clipboardExpand all lines: tests/Symfony/Tests/Component/Validator/Fixtures/Entity.php
+14-18Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,23 @@
66
require_once __DIR__.'/EntityInterface.php';
77

88
/**
9-
* @Validation({
10-
* @NotNull,
11-
* @Symfony\Tests\Component\Validator\Fixtures\ConstraintA,
12-
* @Min(3),
13-
* @Choice({"A", "B"}),
14-
* @All({@NotNull, @Min(3)}),
15-
* @All(constraints={@NotNull, @Min(3)}),
16-
* @Collection(fields={
17-
* "foo" = {@NotNull, @Min(3)},
18-
* "bar" = @Min(5)
19-
* })
9+
* @validation:NotNull
10+
* @Symfony\Tests\Component\Validator\Fixtures\ConstraintA
11+
* @validation:Min(3)
12+
* @validation:Choice({"A", "B"})
13+
* @validation:Validation({
14+
* @validation:All({@validation:NotNull, @validation:Min(3)}),
15+
* @validation:All(constraints={@validation:NotNull, @validation:Min(3)})
16+
* })
17+
* @validation:Collection(fields={
18+
* "foo" = {@validation:NotNull, @validation:Min(3)},
19+
* "bar" = @validation:Min(5)
2020
* })
2121
*/
2222
class Entity extends EntityParent implements EntityInterface
2323
{
2424
/**
25-
* @Validation({
26-
* @Choice(choices={"A", "B"}, message="Must be one of %choices%")
27-
* })
25+
* @validation:Choice(choices={"A", "B"}, message="Must be one of %choices%")
2826
*/
2927
protected $firstName;
3028

@@ -43,12 +41,10 @@ public function getInternal()
4341
}
4442

4543
/**
46-
* @Validation({
47-
* @NotNull
48-
* })
44+
* @validation:NotNull
4945
*/
5046
public function getLastName()
5147
{
5248
return $this->lastName;
5349
}
54-
}
50+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.