2012-10-16 34 views
7

khiểnSymfony2 validation không hoạt động khi Entity mối quan hệ/hiệp hội

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/Loại

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 

Biểu mẫu được tạo hoạt động bình thường. Tuy nhiên, chỉ xác thực không hoạt động bình thường.

Nếu nó hoạt động riêng lẻ, xác thực sẽ hoạt động bình thường.

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

Tuy nhiên, nếu nó đang trong tình trạng mối quan hệ Entity/Hiệp hội, xác nhận đầu tiên sẽ không work.The sở hữu danh hiệu đầu tiên trong một ký tự sẽ được đăng ký.

Làm cách nào tôi có thể quản lý để đạt được điều đó?

+0

Môi trường là Symfony 2.1.2. – JIGEN

+0

Chỉ cần gắn thẻ cho symfony2.1, nếu bạn không nhớ –

Trả lời

14

Symfony 2.1+ không tự động xác thực tất cả các đối tượng được nhúng. Bạn cần phải đặt số Valid constraint vào trường first để làm cho nó hợp lệ:

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

Ồ, tôi hiểu rồi! Nó hoạt động hoàn toàn. Cảm ơn tất cả các lời khuyên và giúp bạn cho tôi. – JIGEN

+2

+1 câu trả lời tuyệt vời @elnur. Sẽ rất tuyệt vời khi thấy điều này trong tài liệu trên phần xác nhận bộ sưu tập. Có lẽ tôi đã bỏ lỡ nó. – Mick

+0

+1, điều này thực sự có thể được tài liệu tốt hơn trong chương xác nhận của cuốn sách – Rvanlaak

Các vấn đề liên quan