bug #11014 [Validator] Remove property and method targets from the optional and required constraints (jakzal)

This PR was merged into the 2.3 branch.

Discussion
----------

[Validator] Remove property and method targets from the optional and required constraints

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |  #11013
| License       | MIT
| Doc PR        |

At the moment both constraints can only be defined on other annotations. Both constraints are [only mentioned in the docs in the context of the Collection](http://symfony.com/doc/current/reference/constraints/Collection.html#required-and-optional-field-constraints).

Defining the required or optional annotation directly on a field or method
throws a ClassNotFoundException, since the constraint validator factory tries to load the validator (which does not exist):

```
ClassNotFoundException: Attempted to load class "OptionalValidator"
from namespace "Symfony\Component\Validator\Constraints"
in /var/www/server/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php line 71.
Do you need to "use" it from another namespace?
```

By applying this patch the end user will get a more helpful error message:

```
[Semantical Error] Annotation @Assert\Optional is not allowed to be declared on property Acme\DemoBundle\Entity\Contact::$message.
You may only use this annotation on these code elements: ANNOTATION.
```

Commits
-------

9c2616e [Validator] Remove property and method targets from the optional and required constraints.
This commit is contained in:
Fabien Potencier 2014-05-31 03:59:25 +02:00
commit b8247a3fc6
2 changed files with 2 additions and 2 deletions

View File

@ -13,7 +13,7 @@ namespace Symfony\Component\Validator\Constraints;
/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
* @Target({"ANNOTATION"})
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/

View File

@ -13,7 +13,7 @@ namespace Symfony\Component\Validator\Constraints;
/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
* @Target({"ANNOTATION"})
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/