-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] form contains no children in PRE_SET_DATA and POST_SET_DATA events #4552
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
Conversation
This means setData() is called twice with data, whereas before, the setData call in the constructor contained null ( symfony/src/Symfony/Component/Form/Form.php Line 232 in 3bdf52a
|
I don't know if this is the way to go but definetely, PRE_SET_DATA events are now called BEFORE the children are added in the builder. My subscriber made some children read_only (now disabled), or not depending on the data present. By default users are not able to change the name, but in some conditions, they can. public function buildForm(FormBuilderInterface $builder, array $options)
{
$subscriber = new ProductFieldsSubscriber($builder->getFormFactory());
$builder->add('name', 'text', array('disabled' => true))
}
Now, in SF 2.1, the presetData function is called before the builder adds its children so the code in the preSetData is pretty useless :( |
@acasademont You should move the You can never assume whether |
I'm in process of upgrading existing 2.0 project to 2.1 and here is the issue I'm facing. We have our own FormType which adds a bunch of fields and a subscriber. The subscriber listens to Extrapolating @bschussek answer above, I can see how to solve this by moving the whole logic of building the form into So, what's the right way of handling this? Also, I have looked into Anyway, I think it's worth mentioning that |
@kostiklv So what way did you use to make what you wanted (I've got the same problem) ? |
IIRC we did the workaround I explained above (i.e. moved the building of the form to PRE_SET_DATA instead of buildForm). @bschussek - would you mind looking into it? |
I think it's a problem. Using the event to add the required fields, besides being unsightly and inappropriate, the field order is lost. To me it makes sense that the form needs to be built before its data are defined. |
I think it's quite necessary to document the form event subscribers' inability to remove form fields somehow: I've wasted a pretty good amount of time trying to figure out why a |
+1 for better documentation or an explanation of how this is supposed to work. It seems that removing form elements in a PRE_SET_DATA listener is only sometimes possible. I have cases where using embedded forms will make removing fields possible, but others where I can't. The use cases should be thoroughly outlined in the documentation so it's clear when you can and can't use this practice. |
For anyone following/finding this issue, it's been resolved here #7878 |
great! |
Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: N/A
Todo: N/A
License of the code: MIT
When the Form constructor was refactored here 2996340 it changed when setData events are fired. They fire before the children are added in the builder here:
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/FormBuilder.php#L206.
This fix calls setData after the children are added so the setData events have access to the form children.