-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form][FormEvents] Not model transformed data bound to preSetData event #7807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is expected behavior. When a form or field receives data from your model (i.e. by calling |
Yes, but form Field also have transformers, so field receive data in model format, but then it should transform data using model transformer since it's attached and pass this data along. This is transformer role:)! |
You are right, but then the data is not in model format anymore, but in normalized respectively view format. You should use the event POST_SET_DATA If you want to access data in transformed format. |
OK, you are right. So the ResizeFormListener should add fields on POST_SET_DATA instead of PRE_SET_DATA. Since to work around this issue I have to create another ResizeFormListener removing all added fields based on not normalized data. Fix is easy, but I'm not sure about breaking logic of code basing on current ResizeFormListener behaviour. |
I suggest to contact the Symfony2 Mailing List for your issue, that's where you usually get a lot of support :) For urgent issues, there is also an IRC #symfony on Freenode. |
This is a bug, so please refer to it. I'll give you detailed example, so you can reproduce it yourself. Model is returning array of entites: [0 => $entity1, 1 => $entity2, 2 => $entity3]. Model transformer split entites to arrays: ['en' => array($entity1), 'fr' => array($entity2, $entity3)] Now what Symfony ResizeFormListener do is creating collection items forms based on not transformed model data resulting in creating forms indexed by: 0, 1; instead of: 'en', 'fr' from transformed data. It's definitely a BUG. And you cannot tell me this behaviour is expected, did you? |
I understand that English may not be your first language (nor is it mine), but please be polite here. I'm trying to help you :) To answer your question: Yes, this behavior is expected. The collection type is not designed to work with associative arrays (i.e. string keys such as 'en' or 'fr'). It is designed to work with numerically indexed collections that can dynamically grow or shrink by adding new elements or removing existing ones. I opened a feature request for the functionality you need: #7828 |
Sorry, for being offensive. I just felt ignored. Thanks, for opening feature request. But this is not exactly the case. My need is to transform flat collection to collection of collections grouped by some key. Synonyms: (1st level collection)
All 2nd level collections are obviously the same type. Only one new failing test is Symfony\Component\Form\Tests\Extension\Core\Type\CollectionTypeTest::testThrowsExceptionIfObjectIsNotTraversable Have no idea for now, how to fix it in nice way. When I figure something out, I'll make pull request. Any suggestions from you side will be appreciated. Thank you for involvement. |
If elements cannot be added, your use case can already be solved right now quite easily: foreach ($languages as $language) {
$builder->add('synonyms_' . $language, 'collection', array(
'type' => 'synonym',
'property_path' => 'synonyms[' . $language . ']',
));
} This will result in one collection field per language code, which will be mapped to the corresponding key in the "synonyms" collection. |
On preSetData data send to the event is not processed by modelTransformer. The easiest case to test is creating collection type with attached model transformer.
Data bound to the event is original data instead of transformed one!
This is required for example when transforming collection from array('key' => 'value') format to array(0 => array('key' => 'original_key', 'value' => 'original_value')).
If collection receive 1st format (wrong - not transformed) instead of 2nd one (good - only model transformed) it created bad names for inputs (based on original key, instead of on transformed ones)
The text was updated successfully, but these errors were encountered: