问题描述
我有一个包含多个字段的表单.
I have a form with several fields.
然后我将 DataTransformer
绑定到其中之一.如果我获取数据 getViewData
和 getNormData
,Transformer 可以正常工作.
Then I bound DataTransformer
to the one of them. Transformer works correctly if I get data getViewData
and getNormData
.
但是如果我在整个表单上发出这些方法,转换器处理的数据将被完全忽略.
But if I issue these methods on whole form, data processed by transformers are completely ignored.
Form
中的数据由于以下原因被覆盖:
In Form
data is being overwritten due to the:
$childrenIterator = new InheritDataAwareIterator($this->children);
$childrenIterator = new \RecursiveIteratorIterator($childrenIterator);
$this->config->getDataMapper()->mapFormsToData($childrenIterator, $viewData);
我的映射器是 PropertyPathMapper
.不幸的是,它基于每个孩子的 getData
方法.
My mapper is PropertyPathMapper
. Unfortunately, it bases on getData
method of each child.
是否有可能绕过 viewData
覆盖/实现正确转换的数据而无需编写全局转换器?
Is it possible to bypass viewData
overwrite / achieve correctly transformed data without writing a global transformer?
推荐答案
解决方法:
$data = array();
foreach ($form->all() as $k => $el) {
$data[$k] = $el->getData(); // or getNormData or getViewData
}
很遗憾我没有解决方案.
Unfortunately I have no solution.
当前的行为不是我所期望的.至少应该在 在 symfony 食谱 中提及.
The current behaviour is not what I expected. It should at least be mentioned in the symfony cookbook.
如您所见,我遇到了同样的问题.我写了一个测试用例,以确保我不会误解变压器的工作原理.我后来发现了你的问题.
As you see I have the same problem. I wrote a test case to be sure that I do not misunderstand how the transformers work. I found your question later.
我的测试用例:https://github.com/SimonHeimberg/SymfonyFormForm_DataTransformer_Demo
这篇关于getViewData 与 getNormData 和 DataTransformers - 根据上下文不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!