2012-10-16 16 views
7

regolatoreconvalida Symfony2 non funziona quando entità relazioni/Associazioni

public function indexAction(Request $request) 
{ 
    $user = $this->container->get('security.context')->getToken()->getUser(); 
    $owner = $user->getId(); 

    $first = new First(); 
    $first->setOwner($owner); 

    $second = new Second(); 
    $second->setOwner($owner); 
    $second->setFirst($first); 

    $form = $this->createForm(new SecondType(), $second); 

    if ($request->getMethod() == 'POST') { 
     $form->bindRequest($request); 

     if ($form->isValid()) { 
      $em = $this->get('doctrine')->getEntityManager(); 
      $em->persist($first); 
      $em->persist($second); 
      $em->flush(); 
     } 
    } 

    return $this->render('MySampleBundle:Home:index.html.twig', array(
     'form' => $form->createView(), 
    )); 

} 

ORM Yaml

My\SampleBundle\Entity\First: 
    type: entity 
    table: first 
    id: 
     id: 
      type: integer 
      generator: { strategy: AUTO } 
    fields: 
     title: 
      type: string 
     date_created: 
      type: datetime 
     date_edited: 
      type: datetime 
     owner: 
      type: integer 
    lifecycleCallbacks: 
     prePersist: [ prePersist ] 
     preUpdate: [ preUpdate ] 
    oneToMany: 
     reviews: 
      targetEntity: Second 
      mappedBy: review 

My\SampleBundle\Entity\Second: 
    type: entity 
    table: second 
    id: 
     id: 
      type: integer 
      generator: { strategy: AUTO } 
    fields: 
     review: 
      type: string 
     date_created: 
      type: datetime 
     date_edited: 
      type: datetime 
     owner: 
      type: integer 
    lifecycleCallbacks: 
     prePersist: [ prePersist ] 
     preUpdate: [ preUpdate ] 
    manyToOne: 
     first: 
      targetEntity: First 
      inversedBy: reviews 
      joinColumn: 
       name: first_id 
       referencedColumnName: id 

Form/Tipo

class FirstType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('title', 'text'); 
    } 

    public function getDefaultOptions(array $options) 
    { 
     return array(
      'data_class' => 'My\SampleBundle\Entity\First', 
     ); 
    } 

    public function getName() 
    { 
     return 'first'; 
    } 
} 

class SecondType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('first', new FirstType()); 
     $builder->add('review', 'textarea'); 
    } 

    public function getName() 
    { 
     return 'second'; 
    } 
} 

Validation.yml

My\SampleBundle\Entity\First: 
    properties: 
     title: 
      - NotBlank: ~ 
      - MinLength: 2 

My\SampleBundle\Entity\Second: 
    properties: 
     review: 
      - NotBlank: ~ 
      - MinLength: 14 

Il modulo creato funziona normalmente. Tuttavia, solo la convalida non funziona normalmente.

Se esegue individualmente, la convalida funzionerà normalmente.

$form = $this->createForm(new FirstType(), $first); 

Tuttavia, se si trova in uno stato di entità relazioni/Associazioni, la prima convalida non work.The proprietà di Primo titolo in un carattere saranno registrati.

Come posso riuscirci?

+0

Ambiente è lo Symfony 2.1.2. – JIGEN

+0

Appena taggato su symfony2.1, se non ti dispiace –

risposta

14

Symfony 2.1+ non convalida automaticamente tutti gli oggetti incorporati. Hai bisogno di mettere il Valid constraint sul campo first per farlo convalidare così:

My\SampleBundle\Entity\Second: 
    properties: 
     first: 
      - Valid: ~ 
+0

Oh, ho capito! Ha funzionato completamente. Grazie per tutti i consigli e l'aiuto che mi avete dato. – JIGEN

+2

+1 ottima risposta @elnur. Sarebbe bello vederlo nella sezione sulla convalida delle collezioni. Forse l'ho perso – Mick

+0

+1, questo potrebbe essere meglio documentato nel capitolo di convalida di The Book – Rvanlaak