PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 23, 2022

[FIXED] Annotation for Symfony validator returns "annotation was never imported exception"

 January 23, 2022     annotations, exception, symfony, validation     No comments   

Issue

I am trying to use the validator service on a Symfony controller, particularly the uniqueEntity constraint to check if an object with a same id is already on the database. I have successfully used de UUID constraint of the validator on the same project before, using annotations as well. For this reason it seems strange that using the same method wouldn't work.

My annotated entity looks like this:

 <?php
//src/Entity/Usuarios.php
    
    namespace App\Entity;
    
    use App\Repository\UsuariosRepository;
    use Doctrine\ORM\Mapping as ORM;
    
    use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
    
    use Symfony\Component\Validator\Constraints as Assert;
    /**
     * @ORM\Entity(repositoryClass=UsuariosRepository::class)
     * @UniqueEntity("idUsuario")
     */
    class Usuarios
    {
        /**
         * @ORM\Id() @ORM\Column(name="idUsuario",type="integer", unique=true) 
         * @Assert\IdUsuario
         */
        private $idUsuario;

\** etc.**\

?>

My controller looks like this:

//src/Entity/Usuarios.php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpClient\HttpClient;
use Doctrine\ORM\EntityManagerInterface;


use App\Entity\Empleados;
use App\Entity\Productos;
use App\Entity\ProductoOrden;
use App\Entity\Ordenes;
use App\Entity\Usuarios;
use App\Entity\Ventas;
use App\Entity\Periodos;


require_once('/Users/jaumaf/clases/2020-1/incentivos/src/testData.php');

use const testData\USUARIOS_T;
use const testData\EMPLEADOS_T;
use const testData\PRODUCTOS_T;
use const testData\ORDENES_T;
use const testData\PERIODOS_T;
use const testData\VENTAS_T;

use Psr\Log\LoggerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;



class LoadDataController extends AbstractController
{
    /**
     * @Route("/load/data", name="load_data")
     */
  public function index(LoggerInterface $logger, ValidatorInterface $validator)  {

  //los json estan en testData.php

   $usuariosArray = json_decode(USUARIOS_T, TRUE);
   $empleadosArray = json_decode(EMPLEADOS_T,TRUE);
   $productosArray = json_decode(PRODUCTOS_T,TRUE);
   $ordenesPorEmpleadoArray = json_decode(ORDENES_T,TRUE);
   $periodosArray = json_decode(PERIODOS_T, TRUE);
   $ventasPorVendedorArray= json_decode(VENTAS_T, TRUE);

   $logger->info('Test constants loaded');

     
  $entities = $this -> mapArraysToObjects($usuariosArray, $empleadosArray, $productosArray,  $ordenesPorEmpleadoArray,$ventasPorVendedorArray,$periodosArray);
  $logger->info('Entities loaded to memory');

  $cont = 0;
  $logtable =  $this -> logLoadedEntities($cont, $entities);
  

  // Persistencia
  $entityManager = $this->getDoctrine()->getManager();

  $logger->info('ORM loaded');


  // Validator test

  $usuario = $entities[0];


  $validation = $validator -> validate($usuario);

// this should make the validator throw a uniqueEntity exception, but it throws an annotation exception. 

The exception I am getting is:

[Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\IdUsuario" in property App\Entity\Usuarios::$idUsuario was never imported. Did you maybe forget to add a "use" statement for this annotation?

I have tried adding a use statement for Symfony\Component\Validator\Constraints\IdUsuario on my controller as the exception message seems to imply (however, the Symfony guide doesn't mention this step so it should be unneccesary). But it shows exactly the same error.

I also tried making a doctrine migration hoping that the metadata the annotations use somehow updates itself, but It throws the same exception in the command line.


bash-3.2$  php bin/console make:migration

In AnnotationException.php line 54:
                                                                                     
  [Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\IdUsu  
  ario" in property App\Entity\Usuarios::$idUsuario was never imported. Did you may  
  be forget to add a "use" statement for this annotation?                            
                                                             

I am new to php and I don't really understand how annotations work. But I've followed the guide in https://symfony.com/doc/current/reference/constraints/UniqueEntity.html. I'm pretty sure I've gone over the syntax thoroughly and as I said it worked before with a different constraint. In addition, the exception is thrown from within the the controller when I make a call to the validate() function. Maybe there is something I'm missing in the controller side or something on the Symfony framework system that I don't know about that might affect the behavior of the validation service?

I would appreciate any guidance. I will be trying to wrap my head over annotations on the meantime.

Thanks


Solution

Remove annotation @Assert\IdUsuario from class Usuarios

You copy-pasted it from https://symfony.com/doc/current/reference/constraints/UniqueEntity.html It's not related to UniqueEntity validation. It's just checking that field is a valid email.



Answered By - Leprechaun
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing