Php 访问窗体中collectionType中的嵌入字段
我想做的是: 我正在尝试从集合中的嵌入formType访问字段。 我可以通过Php 访问窗体中collectionType中的嵌入字段,php,symfony,doctrine,Php,Symfony,Doctrine,我想做的是: 我正在尝试从集合中的嵌入formType访问字段。 我可以通过$form->get('childType')轻松访问第一级(因此获取集合),但我很难访问嵌入在childType中的字段。 我尝试了$form->get('childType')->get('anotherAttr'),但没有成功。我认为问题的根源在于,一个集合不仅仅是一个字段,如果Symfony不知道我想获取集合中的哪一项,则无法获取('anotherAttr')。无论如何,经过大量的搜索,我还没有找到如何告诉他我想
$form->get('childType')
轻松访问第一级(因此获取集合),但我很难访问嵌入在childType中的字段。
我尝试了$form->get('childType')->get('anotherAttr')
,但没有成功。我认为问题的根源在于,一个集合不仅仅是一个字段,如果Symfony不知道我想获取集合中的哪一项,则无法获取('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';
}
}
多亏了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();
您能给我们看一下控制器代码吗?将控制器代码添加到问题的正文中。如果您在发布数据时
var\u dump($form->get('childType'))
会怎么样?我不明白你什么时候使用$individu->addChildType(new ChildType())代码>:对象$individu
来自哪里?当您创建表单
时,您给它新的父类型
,并作为第二个参数$ParentType
,这是相同的。您应该传递一个my\myBundle\Entity\Parent
对象。这是parentTypeAddAction()
的完整代码吗?很抱歉,我试图使我的代码更通用/更可重用,我只是打字-\uu-是的,这是parentTypeAddAction()的完整代码。我纠正了那些打字错误$form->get('childType'))据我所知是一个表单集合(我明天会var_dump,这里没有代码)好的,非常感谢,我不知道var_dump有那么强大。请注意,如果使用PHP>5.4,可以重写$childAttr=$form->get('childType')->getChildren()[0]->get('childAttr')代码>。这很好,我不知道。谢谢所有的@cheesemacfly。
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()));
}
//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();