0

我想要做什么:我正在尝试从集合中嵌入的 formType 访问字段。我可以轻松访问第一级(因此获取集合),$form->get('childType')但我很难访问嵌入在 childType 中的字段。我试过$form->get('childType')->get('anotherAttr')没有成功。恕我直言,问题来自这样一个事实,即 Collection 不仅仅是一个字段,如果 Symfony 不知道我想要获取的集合的哪个项目,就无法完成 get('anotherAttr') 。无论如何,经过大量搜索后,我还没有找到如何告诉他我想要收藏中的第一个项目。

这是代码:

父类类型:

<?php

namespace my\myBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class ParentType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('attribute1','text',array("label" =>     'attribute 1 :'))
                ->add('childType','collection',array('type' => new ChildType($options['attrForChild'])));
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'my\myBundle\Entity\Parent',
            'attrForChild'         => null
        );
    }

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

子类类型:

 <?php

namespace my\myBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

    class ChildType extends AbstractType
    {
        private $childAttr;

    public function __construct($childAttr=null){
        $this->childAttr=$childAttr;
    }

    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('childAttr','text',array("label" => 'childAttr : ','property_path' => false));
                if(isset($this->childAttr)){
                    $childAttr = $this->childAttr;
                    $builder->add('childAttrDependantEntity','entity',array("label" => 'RandomStuff : ',
                        'class' => 'mymyBundle:randomEntity',
                        'property' => 'randProperty',
                        'multiple' => false,
                        'query_builder' => function(\my\myBundle\Entity\randomEntityRepository $r) use ($childAttr) {
                            return $r->findByChildAttr($childAttr);
                        }
                    ));
                }
                $builder->add('anotherAttr','text',array("label" => 'Other attr : '))
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'crri\suapsBundle\Entity\Adresse',
            'childAttr'         => null
        );
    }

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

另外,我使用的 childAttr 解决方案可以吗?(它正在工作,但感觉有点像黑客,有没有更清洁的方法来做同样的事情?)。它的用途=用户给了我一个文本字段,我验证它是否存在于数据库中,如果它确实存在,我将一个 entityType 添加到与此属性相关的表单中。目标是用户将从限制元素列表中选择,而不是从数据库中选择所有元素。

编辑:控制器的相应代码:

public function parentTypeAddAction(Request $request){
    $parentEntity = new ParentEntity();
    $parentEntity->addChildEntity(new ChildEntity());
    $form = $this->createForm(new ParentType,$parentEntity);
    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);
        // Testing (everything I tried)
        $test=$form->get('childType')->getAttribute('childAttr');
        /**
        $test=$form['childAttr'];
        $test=$form->get('childAttr'); **/
        return $this->container->get('templating')->renderResponse('myMyBundle:Default:test.html.twig',
                array('test' => $test));
        if($test!=null ){
                $anEntity = $em->getRepository('crrisuapsBundle:AnEntity')->find($test);
                if($anEntity==null){
                    $form->get('childType')->get('childAttr')->addError(new FormError("Invalid attribute."));
                } else {
                    $form = $this->createForm(new ParentType,$parentType,array('childAttr' => $test));
                    $individu->getAdresses()->first()->setAnEntity($anEntity);
                }
            }
        $form->bindRequest($request);
        if($request->request->get('CHILDATTRPOST')!='Search attribute'){
            if ($form->isValid()) {
                $em->persist($parentType);
                $em->persist($individu->getChildEntity()->first());
                $em->flush();
                return $this->redirect($this->generateUrl('myMyBundle_homepage'), 301);
            }
        }
    }
    return $this->container->get('templating')->renderResponse('myMyBundle:Default:parentTypeAdd.html.twig', 
            array('form' => $form->createView()));
}
4

1 回答 1

1

感谢 cheesemacfly 的建议,我可以弄清楚如何获得它。这是解决方案:

//Getting the childEntities forms as an array
$childArray=$form->get('childType')->getChildren();
//Getting the childEntity form you want
$firstChild=$childArray[0];
//Getting your attribute like any form
$childAttrForm=$childArray[0]->get('childAttr');
$childAttr=$childAttrForm->getData();
于 2013-05-07T07:13:22.180 回答