【问题标题】:Symfony 3 - Use same EntityType as child entry Type in a CollectionTypeSymfony 3 - 使用与 CollectionType 中的子条目类型相同的 EntityType
【发布时间】:2017-10-02 12:16:58
【问题描述】:

在我的 symfony 3.3.2 项目中,我试图在 FormBuilder 内的 CollectionType 中将父实体用作嵌入表单中的 ChildType:

class PieceType extends AbstractType
{

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('reference' , TextType::class , array(
              'required' => true,
              ))
            ->add('designation' , TextType::class, array(
              'required' => true,
            ))
            ->add('prix' , NumberType::class, array(
              'required' => true,
            ))
            ->add('quantite' , NumberType::class)
            ->add('etat' )

            ->add('equivalents' ,  CollectionType::class , array(
                  'entry_type'   => PieceType::class ,
                  'allow_add' => true,
                  'allow_delete' => true,
                  'prototype' => true,
                  'attr' => array(
                     'class' => 'pieces_form-collection',
                 ),
            ))


            ->add('caracteristiques' ,  CollectionType::class , array(
                  'entry_type'   => CaracteristiqueType::class ,
                  'allow_add' => true,
                  'allow_delete' => true,
                  'prototype' => true,
                  'attr' => array(
                     'class' => 'caract_form-collection',
                 ),
            ))

    }
}

当我使用 PieceType 作为 childType 时,项目不起作用,我只得到白页。 有什么建议吗?

【问题讨论】:

    标签: doctrine-orm symfony-forms symfony-3.3


    【解决方案1】:

    我通过在控制器中添加我的 childType 来修复它:

    public function editAction(Request $request, Piece $piece)
    {
        $editForm = $this->createForm('EK\AdministrationBundle\Form\PieceType', 
        $piece);
    
        $form = $editForm
                ->add('equivalents' , CollectionType::class , array(
                      'entry_type'   => PieceType::class ,
                      'allow_add' => true,
                      'allow_delete' => true,
                      'prototype' => true,
                      'attr' => array(
                         'class' => 'pieces_form-collection',
                     ),
        ));
    
        return $this->render('EKAdministrationBundle:Piece:edit.html.twig', 
        array('edit_form' => $form->createView(), ));
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-01
      • 1970-01-01
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多