symfonydoctrine-ormsymfony4symfony-validator

Symfony4 : The annotation does not exist, or could not be auto-loaded |@Assert\NotBlank()|


When I try to drop database schema by:

$ vendor/bin/doctrine orm:schema-tool:drop --force

I keep getting this error:

In AnnotationException.php line 54:
[Semantical Error] The annotation 
"@Symfony\Component\Validator\Constraints  
\NotBlank" in property App\Entity\User::$plainPassword does not exist, 
or could not be auto-loaded. 

This is my User.php:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;


/**
* @ORM\Table(name="app_users")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/

class User implements UserInterface, \Serializable
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string", length=25, unique=true)
 */
private $username;

/**
 * @Assert\NotBlank()
 * @Assert\Length(max=4096)
 */
private $plainPassword;


/** @ORM\Column(type="string", length=64)
 */
private $password;

/**
 * @ORM\Column(type="string", length=60, unique=true)
 */
private $email;

/**
 * @ORM\Column(name="is_active", type="boolean")
 */
private $isActive;

/** @ORM\Column(type="datetime") * */
protected $created_at;

/** @ORM\Column(type="datetime", nullable=true) * */
protected $last_login;

/**
 * @var Video[]
 * @ORM\OneToMany(targetEntity="Video", mappedBy="uploadedBy")
 */
protected $videos;

public function __construct()
{
    $this->isActive = true;
    $this->videos = new ArrayCollection();
}


public function getId()
{
    return $this->id;
}

public function getUsername(): ?string //php7.1
{
    return $this->email;
}

public function getSalt() //Not necessary
{
    return null;
}

public function getPlainPassword()
{
    return $this->plainPassword;
}

public function getPassword()
{
    return $this->password;
}

public function getEmail()
{
    return $this->email;
}

public function getRoles()
{
    return array('ROLE_USER');
}

public function eraseCredentials()
{
}

/** @see \Serializable::serialize() */
public function serialize()
{
    return serialize(array(
        $this->id,
        $this->username,
        $this->password,
        $this->isActive,
    ));
}

/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
    list (
        $this->id,
        $this->username,
        $this->password,
        $this->isActive,
    ) = unserialize($serialized);
}

public function getCreatedAt()
{
    return $this->created_at;
}

public function getLastLogin()
{
    return $this->last_login;
}

public function setUsername(string $username) //php7
{
    $this->username = $username;
}

public function setEmail($email)
{
    $this->email = $email;
}

public function setPlainPassword($password)
{
    $this->plainPassword = $password;
}

public function setPassword($password)
{
    $this->password = $password;
}

public function setCreatedAt($created_at)
{
    $this->created_at = $created_at;
}

public function setLastLogin($last_login)
{
    $this->created_at = $last_login;
}

public function addVideo(Video $video)
{
    if (!$this->hasVideo($video)) {
        $this->videos->add($video);
    }
}

public function deleteVideo(Video $video)
{
    if ($this->hasVideo($video)) {
        $this->videos->removeElement($video);
    }
}

public function hasVideo(Video $video)
{
    return $this->videos->contains($video);
}

public function getVideos()
{
    return $this->videos;
}

public function isAccountNonExpired()
{
    return true;
}

public function isAccountNonLocked()
{
    return true;
}

public function isCredentialsNonExpired()
{
    return true;
}

public function isEnabled()
{
    return $this->isActive;
}
}

I tried to solve the problem this way:

Symfony4 : The annotation does not exist, or could not be auto-loaded (Symfony\Component\Validator\Constraints)

That mean I already have "symfony/validator": "^4.0" installed. But it doesn't work any sugesstions?


Solution

  • Check the FrameworkBundle Configuration ("framework") validation annotation are enabled. See in the files config/packages/framework.yaml the options enable_annotations under validation is correctly set to true (the dafault is false)

    As example:

    framework:
        secret: '%env(APP_SECRET)%'
        validation:      { enable_annotations: true }
    

    Hope this help