symfony 2.3 form getData在子表单集合中不起作用 [英] symfony 2.3 form getData doesn't work in subforms collections
问题描述
我有一个包含集合的表单。所以我有:
I have a form which contains a collection. So I have:
/* my type */
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('photos','collection',array(
'type'=> new PhotoType(),
'allow_add'=>true));
}
/*Photo Type*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('photoname')
->add('size')
}
b $ b
但我想访问照片中的数据,所以我尝试了PhotoType内部:
But I want to access the data inside the photo, so I tried inside the PhotoType:
$data = $builder->getData();
但是似乎它不工作,即使我编辑表单,集合有数据。
为什么我不能访问$ builder-> getData()在一个窗体调用另一个?因为我不想做和eventListener ...
But it seems that it doesn't work, even if I am editting the form, so the photo collection has data. Why can't I access to the $builder->getData() in a form called by another?? Because I'm trying not to do and eventListener...
推荐答案
要理解这里发生的事情你必须了解数据映射第一。当您调用
To understand what is happening here you have to understand data mapping first. When you call
$form->setData(array('photoname' => 'Foobar', 'size' => 500));
表单的数据映射器负责获取给定的数组)和将嵌套值写入表单的字段,即调用
the form's data mapper is responsible for taking the given array (or object) and writing the nested values into the fields of the form, i.e. calling
$form->get('photoname')->setData('Foobar');
$form->get('size')->setData(500);
但在你的例子中,你不是处理 Form
,但使用 FormBuilder
对象。 FormBuilder
负责收集表单的配置,并使用此信息生成表单
实例。因此, FormBuilder
也可以存储表单的默认数据。但由于它只是一个简单的配置对象,它将不会调用数据映射器。例如:
But in your example, you are not dealing with Form
, but with FormBuilder
objects. FormBuilder
is responsible for collecting the configuration of a form and using this information to produce a Form
instance. As such, FormBuilder
also lets you store the default data for the form. But since it's a simple configuration object only, it will not invoke the data mapper as of yet. For example:
$builder = $factory->createBuilder()
->add('photoname')
->add('size')
->setData(array('photoname' => 'Foobar', 'size' => 500));
print_r($builder->get('photoname')->getData());
print_r($builder->get('size')->getData());
此示例将输出:
null
null
当我们将 FormBuilder
变成 Form
实例时。我们可以使用这个事实为单个字段设置单独的默认值:
because data mapping takes place later, when we turn the FormBuilder
into a Form
instance. We can use this fact to set separate default values for the individual fields:
$builder->add('size', null, array('data' => 100));
// which is equivalent to
$builder->get('size')
->setData(100)
->setDataLocked(true);
print_r($builder->get('photoname')->getData());
print_r($builder->get('size')->getData());
输出:
null
100
需要数据锁定才能防止数据mapper覆盖刚刚存储的默认数据。如果您传递data选项,则会自动完成。
Data locking is required to prevent the data mapper from overriding the default data you just stored. This is done automatically if you pass the "data" option.
最后,您将构建表单。现在, FormBuilder
在必要时调用 Form :: setData()
,这将调用数据映射器: p>
At last, you will build the form. Now, FormBuilder
calls Form::setData()
where necessary, which in turn will invoke the data mapper:
$form = $builder->getForm();
// internally, the following methods are called:
// 1) because of the default data configured for the "size" field
$form->get('size')->setData(100);
// 2) because of the default data configured for the main form
$form->setData(array('photoname' => 'Foobar', 'size' => 500));
// 2a) as a result of data mapping
$form->get('photoname')->setData('Foobar');
// 2b) as a result of data mapping (but ignored, because the data was locked)
$form->get('size')->setData(500);
这篇关于symfony 2.3 form getData在子表单集合中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!