Problème validation entité UniqueEntity
Bonsoir,
Je souhaite gérer la validation des entités d'une table sur le risque de doublon sur un champ. Aucune des documentations sur le sujet ne me donne une vraie réponse.
J'utilise une configuration yml complète.
Il me semble avoir fait le nécessaire et pourtant, les tests de doublons sur le champ "code" ne fonctionnent pas : les doublons s'accumulent dans le base
Manque-t-il une config ?
voici ma config :
fichier config.yml:
Code:
1 2
| framework:
validation: { enabled: true } |
entité origine.orm.yml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| Activite\ContactsBundle\Entity\Origine:
type: entity
table: null
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
code:
type: string
length: '15'
unique: true
notnull: true
libelle:
type: string
length: '30'
CategorieOrigine:
type: object
length: null
lifecycleCallbacks: { } |
validation.yml
Code:
1 2 3 4 5 6 7 8
| # src/Activite/ContactsBundle/Resources/config/validation.yml
Acme\ContactsBundle\Entity\Origine:
constraints:
Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: code
properties:
code:
- text: ~
- message: "code existe." |
origine.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| <?php
namespace Activite\ContactsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* Activite\ContactsBundle\Entity\Origine
*/
class Origine
{
/**
* @var integer $id
*/
private $id;
/**
* @var string $code
*/
private $code;
/**
* @var string $libelle
*/
private $libelle;
/**
* @var object $CategorieOrigine
*/
private $CategorieOrigine;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set code
*
* @param string $code
*/
public function setCode($code)
{
$this->code = $code;
}
/**
* Get code
*
* @return string
*/
public function getCode()
{
return $this->code;
}
/**
* Set libelle
*
* @param string $libelle
*/
public function setLibelle($libelle)
{
$this->libelle = $libelle;
}
/**
* Get libelle
*
* @return string
*/
public function getLibelle()
{
return $this->libelle;
}
/**
* Set CategorieOrigine
*
* @param object $categorieOrigine
*/
public function setCategorieOrigine($categorieOrigine)
{
$this->CategorieOrigine = $categorieOrigine;
}
/**
* Get CategorieOrigine
*
* @return object
*/
public function getCategorieOrigine()
{
return $this->CategorieOrigine;
}
} |
origineController.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| <?php
namespace Activite\ContactsBundle\Controller;
use Activite\ContactsBundle\Entity\Origine;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Activite\ContactsBundle\Form\OrigineType;
class OrigineController extends Controller
{
public function AjouterOrigineFormAction()
{
$origine= new Origine();
$form = $this->get('form.factory')->create(new OrigineType(),$origine);
$request = $this->get('request');
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
$libelle=$origine->getLibelle();
//créer enregistrement origine
$em = $this->getDoctrine()->getEntityManager();
$em->persist($origine);
$em->flush();
$message="L'origine ".$libelle. " vient d'être créée.";
//router vers liste des origines
return $this->redirect($this->generateUrl('ActiviteContactsBundle_Origine_ajouter'));
}
}
return $this->render('ActiviteContactsBundle:Default:formAjout.html.twig',
array('form' => $form->createView(),
);
}
} |