title: Validation annotations in doctrine entities
body: <p>Hi I have a symfony project where I have controller called CompanyController. 
  I have there function for saving new Company based on some data which I send by POST method. </p>
  
  <p>Below I show this method:</p>
  
  <pre><code>/**
       * @param Request $request
       * @param ValidatorInterface $validator
       * @return string|Response
       */
      public function store(Request $request, ValidatorInterface $validator) {
                  $company = new Company();
                  $company-&gt;setName($request-&gt;get('name'));
                  $company-&gt;setTaxNumber($request-&gt;get('taxNumber'));
                  $company-&gt;setStreet($request-&gt;get('street'));
                  $company-&gt;setCity($request-&gt;get('postalCode'));
                  $company-&gt;setOfficeId($request-&gt;get('officeId'));
                  $company-&gt;setPostalCode($request-&gt;get('postalCode'));
                  $errors = $validator-&gt;validate($company);
                  if(count($errors) &gt; 0) {
                      /**
                       * some code
                       */
                  }
          /**
           * some response
           */
      }
  </code></pre>
  
  <p>In this method I'm trying to validate data from POST response. I wonder if is it good practise to put annotation for validation in entity file Companies like below: </p>
  
  <pre><code>&lt;?php
  
  namespace App\Entity;
  
  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\Common\Collections\Collection;
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Validator\Constraints as Assert;
  
  /**
   * @ORM\Entity(repositoryClass="App\Repository\CompanyRepository")
   */
  class Company
  {
      /**
       * @ORM\Id()
       * @ORM\GeneratedValue()
       * @ORM\Column(type="integer")
       */
      private $id;
  
      /**
       * @ORM\Column(type="string", length=255, nullable=true)
       * @Assert\NotBlank
       */
      private $name;
  
      /**
       * @ORM\Column(type="string", length=255)
       * @Assert\NotBlank
       */
      private $taxNumber;
  
      /**
       * @ORM\Column(type="string", length=255, nullable=true)
       * @Assert\NotBlank
       */
      private $street;
  
      /**
       * @ORM\Column(type="string", length=255)
       * @Assert\NotBlank
       */
      private $city;
  
      /**
       * @ORM\Column(type="string", length=255)
       * @Assert\NotBlank
       */
      private $postalCode;
  
      /**
       * @ORM\Column(type="string", length=255)
       * @Assert\NotBlank
       *
       */
      private $officeId;
  
      /**
       * @var datetime $created
       *
       * @ORM\Column(type="datetime")
       */
      protected $createdAt;
  
      /**
       * @var datetime $updated
       *
       * @ORM\Column(type="datetime", nullable = true)
       */
      protected $updatedAt;
  
      /**
       * @ORM\OneToMany(targetEntity="App\Entity\Employee", mappedBy="company")
       */
      private $employees;
  
      public function __construct()
      {
          $this-&gt;employees = new ArrayCollection();
      }
  
  
      /**
       * Gets triggered only on insert
       * @ORM\PrePersist
       */
      public function onPrePersist()
      {
          $this-&gt;createdAt = new \DateTime("now");
      }
  
      /**
       * Gets triggered every time on update
       * @ORM\PreUpdate
       */
      public function onPreUpdate()
      {
          $this-&gt;updatedAt = new \DateTime("now");
      }
  
  
      public function getId(): ?int
      {
          return $this-&gt;id;
      }
  
      public function getName(): ?string
      {
          return $this-&gt;name;
      }
  
      public function setName(?string $name): self
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      public function getTaxNumber(): ?string
      {
          return $this-&gt;taxNumber;
      }
  
      public function setTaxNumber(string $taxNumber): self
      {
          $this-&gt;taxNumber = $taxNumber;
  
          return $this;
      }
  
      public function getStreet(): ?string
      {
          return $this-&gt;street;
      }
  
      public function setStreet(?string $street): self
      {
          $this-&gt;street = $street;
  
          return $this;
      }
  
      public function getCity(): ?string
      {
          return $this-&gt;city;
      }
  
      public function setCity(string $city): self
      {
          $this-&gt;city = $city;
  
          return $this;
      }
  
      public function getPostalCode(): ?string
      {
          return $this-&gt;postalCode;
      }
  
      public function setPostalCode(string $postalCode): self
      {
          $this-&gt;postalCode = $postalCode;
  
          return $this;
      }
  
      public function getOfficeId(): ?string
      {
          return $this-&gt;officeId;
      }
  
      public function setOfficeId(string $officeId): self
      {
          $this-&gt;officeId = $officeId;
  
          return $this;
      }
  
      /**
       * @return Collection|Employee[]
       */
      public function getEmployees(): Collection
      {
          return $this-&gt;employees;
      }
  
      public function addEmployee(Employee $employee): self
      {
          if (!$this-&gt;employees-&gt;contains($employee)) {
              $this-&gt;employees[] = $employee;
              $employee-&gt;setCompany($this);
          }
  
          return $this;
      }
  
      public function removeEmployee(Employee $employee): self
      {
          if ($this-&gt;employees-&gt;contains($employee)) {
              $this-&gt;employees-&gt;removeElement($employee);
              // set the owning side to null (unless already changed)
              if ($employee-&gt;getCompany() === $this) {
                  $employee-&gt;setCompany(null);
              }
          }
  
          return $this;
      }
  }
  </code></pre>
  
  <p>Is this the correct approach? Second approach I've met i to put annotation in completely different Entity file. For example: </p>
  
  <pre><code>public function productPostAction(AccessChecker $checker, Request $request, JsonFromDbObjectConverter $converter){
          $productRequest = new ProductRequest();
                  $productRequest-&gt;name = $request-&gt;get('name');
                  $productRequest-&gt;users = $request-&gt;get('users');
                  $productRequest-&gt;llc = (int)$request-&gt;get('llc');
  
                  $validator = $this-&gt;get('validator');
                  $errors = $validator-&gt;validate($productRequest);
  
                  if (count($errors) &gt; 0) {
                          /**
                          * some code
                          */
  
                  }
                  /**
                  * some response
                  */
  }
  </code></pre>
  
  <p>Separate Entity class for validation:</p>
  
  <pre><code>&lt;?php
  
  namespace CblBundle\Request;
  
  use Symfony\Component\Validator\Constraints as Assert;
  
  class ProductRequest
  {
      /**
       * @var string
       *
       * @Assert\NotBlank()
       */
      public $name;
  
      /**
       * @var array
       */
      public $users;
  
      /**
       * @var int
       * @Assert\Type("integer")
       */
      public $llc;
  
      /**
       * @Assert\IsTrue(message = "Wrong users data!")
       */
      public function isUsersTypeOfNullOrArray()
      {
          return (is_null($this-&gt;users) || is_array($this-&gt;users));
      }
  }
  </code></pre>