Symfony: Validator AnnotationException Constraint\Type could not be autoloaded

Created on 8 Jul 2016  路  3Comments  路  Source: symfony/symfony

I'm trying to create a prototype of using the Validator component as a stand alone component with the use of annotations. I'm getting an error:

PHP Fatal error:  Uncaught Doctrine\Common\Annotations\AnnotationException:
[Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\Type" in
property BB\Prototype\AssertExample::$stringProperty does not exist, or could not be
auto-loaded. in 
/Users/jpresley/projects/prototype/vendor/doctrine/annotations/lib/Doctrine/Common/
Annotations/AnnotationException.php:54

I'm using this as the base of my prototype.

My code is as follows:
src/AssertExample.php

<?php

namespace BB\Prototype;

use Symfony\Component\Validator\Constraints as Assert;

/**
 * Class AssertExample
 * @package BB\Prototype
 */
class AssertExample
{
    /**
     * @Assert\Type("string")
     */
    protected $stringProperty;

    public function __construct($stringProperty)
    {
        $this->stringProperty = $stringProperty;
    }

    public function getStringProperty()
    {
        return $this->stringProperty;
    }
}

index.php

<?php

use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints as Assert;
use BB\Prototype\AssertExample;

require "vendor/autoload.php";

//new Assert\Type("string");
$validator = Validation::createValidatorBuilder()
    ->enableAnnotationMapping()
    ->getValidator();

$assertExampleBad = new AssertExample(123);
$errors = $validator->validate($assertExampleBad);
foreach ($errors as $error) {
    echo $error->getPropertyPath(), ': ', $error->getMessage(), PHP_EOL;
    // or for debugging:
    echo $error, PHP_EOL;
}
$validAssertExample = new AssertExample('[email protected]');
$errors = $validator->validate($validAssertExample);
echo count($errors), PHP_EOL;

composer.json

{
    "name": "jpresley/prototype",
    "description": "Project for prototyping",
    "require": {
        "symfony/validator": "^3.1",
        "doctrine/annotations": "^1.2",
        "doctrine/cache": "^1.6"
    },
    "authors": [
        {
            "name": "jpresley",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "stable",
    "autoload": {
        "psr-4": {
            "BB\\Prototype\\": "src"
        }
    }
}


See the full stacktrace

$ php index.php

PHP Fatal error:  Uncaught Doctrine\Common\Annotations\AnnotationException: [Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\Type" in property BB\Prototype\AssertExample::$stringProperty does not exist, or could not be auto-loaded. in /Users/jpresley/projects/prototype/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php:54
Stack trace:
#0 /Users/jpresley/projects/prototype/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php(708): Doctrine\Common\Annotations\AnnotationException::semanticalError('The annotation ...')
#1 /Users/jpresley/projects/prototype/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php(641): Doctrine\Common\Annotations\DocParser->Annotation()
#2 /Users/jpresley/projects/prototype/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php(334): Doctrine\Common\Annotations\DocParser->Annotations()
#3 /Users/jpresley/projects/prototype/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/A in /Users/jpresley/projects/prototype/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php on line 54

Fatal error: Uncaught Doctrine\Common\Annotations\AnnotationException: [Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\Type" in property BB\Prototype\AssertExample::$stringProperty does not exist, or could not be auto-loaded. in /Users/jpresley/projects/prototype/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php on line 54

Doctrine\Common\Annotations\AnnotationException: [Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\Type" in property BB\Prototype\AssertExample::$stringProperty does not exist, or could not be auto-loaded. in /Users/jpresley/projects/prototype/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php on line 54

Call Stack:
    0.0002     355264   1. {main}() /Users/jpresley/projects/prototype/index.php:0
    0.0088    1020680   2. Symfony\Component\Validator\Validator\RecursiveValidator->validate() /Users/jpresley/projects/prototype/index.php:15
    0.0108    1213840   3. Symfony\Component\Validator\Validator\RecursiveContextualValidator->validate() /Users/jpresley/projects/prototype/vendor/symfony/validator/Validator/RecursiveValidator.php:117
    0.0110    1215824   4. Symfony\Component\Validator\Validator\RecursiveContextualValidator->validateObject() /Users/jpresley/projects/prototype/vendor/symfony/validator/Validator/RecursiveContextualValidator.php:154
    0.0110    1215824   5. Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory->getMetadataFor() /Users/jpresley/projects/prototype/vendor/symfony/validator/Validator/RecursiveContextualValidator.php:340
    0.0119    1282600   6. Symfony\Component\Validator\Mapping\Loader\AnnotationLoader->loadClassMetadata() /Users/jpresley/projects/prototype/vendor/symfony/validator/Mapping/Factory/LazyLoadingMetadataFactory.php:126
    0.0129    1308552   7. Doctrine\Common\Annotations\CachedReader->getPropertyAnnotations() /Users/jpresley/projects/prototype/vendor/symfony/validator/Mapping/Loader/AnnotationLoader.php:62
    0.0129    1309496   8. Doctrine\Common\Annotations\AnnotationReader->getPropertyAnnotations() /Users/jpresley/projects/prototype/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/CachedReader.php:117
    0.0129    1310440   9. Doctrine\Common\Annotations\DocParser->parse() /Users/jpresley/projects/prototype/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationReader.php:226
    0.0130    1311192  10. Doctrine\Common\Annotations\DocParser->Annotations() /Users/jpresley/projects/prototype/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php:334
    0.0130    1311248  11. Doctrine\Common\Annotations\DocParser->Annotation() /Users/jpresley/projects/prototype/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php:641

The odd thing is that if I uncomment new Assert\Type("string"); the validator works. It doesn't matter what I put in for the argument, e.g. new Assert\Type("email");, it will still work.

Bug Needs Review Unconfirmed Validator

Most helpful comment

@jpresley23 Your issue is very hard to read, it would be great if you could use some markdown styling options e.g. syntax highlighting. https://guides.github.com/features/mastering-markdown/

All 3 comments

@jpresley23 Your issue is very hard to read, it would be great if you could use some markdown styling options e.g. syntax highlighting. https://guides.github.com/features/mastering-markdown/

Did you configure the AnnotationRegistry (see https://github.com/symfony/symfony-standard/blob/master/app/autoload.php for an example)?

@xabbuh That fixed it. Thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

garak picture garak  路  3Comments

tdesvenain picture tdesvenain  路  3Comments

FRAGnatt picture FRAGnatt  路  3Comments

sunviwo picture sunviwo  路  3Comments

tomaszhanc picture tomaszhanc  路  3Comments