Skip to content

Commit

Permalink
bug symfony#11014 [Validator] Remove property and method targets from…
Browse files Browse the repository at this point in the history
… 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 |  symfony#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.
  • Loading branch information
fabpot committed May 31, 2014
2 parents cff4105 + 9c2616e commit b8247a3
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Validator/Constraints/Optional.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
* @Target({"ANNOTATION"})
*
* @author Bernhard Schussek <[email protected]>
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Validator/Constraints/Required.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
* @Target({"ANNOTATION"})
*
* @author Bernhard Schussek <[email protected]>
*/
Expand Down

0 comments on commit b8247a3

Please sign in to comment.