Skip to content

BC break for collection type's prototype_data feature #15707

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

Closed
backbone87 opened this issue Sep 7, 2015 · 13 comments
Closed

BC break for collection type's prototype_data feature #15707

backbone87 opened this issue Sep 7, 2015 · 13 comments
Labels

Comments

@backbone87
Copy link
Contributor

related PR: #12314
related commit: 54bd6f7

the way its currently implemented in this commit is a BC break since the prototype_data will always be used, even if legacy code not using prototype_data relies on the old behavior where the data option is used for the prototype form. i recommend revisiting this implementation and using code similar to the one i showed in my comment in the PR #12314 (comment)

@xabbuh xabbuh added the Form label Oct 5, 2015
@xabbuh
Copy link
Member

xabbuh commented Oct 5, 2015

@backbone87 Could you provide a failing test case?

@jakzal
Copy link
Contributor

jakzal commented Oct 15, 2015

@backbone87 ping

@backbone87
Copy link
Contributor Author

Sorry, i am really busy atm.
Derived from the test case used in the PR:

public function testLegacyPrototypeData() {
  $form = $this->factory->create('collection', array(), array(
    'type' => 'text',
    'allow_add' => true,
    'prototype' => true,
    //'prototype_data' => 'foo', // in a legacy form this is not present
    'options' => array(
      'data' => 'bar',
    ),
  ));

  // this will fail, but is expected legacy behavior
  $this->assertSame('bar', $form->createView()->vars['prototype']->vars['value']);
}

I didnt actually run this test case, but it should be pretty obvious that it fails.

@memphys
Copy link
Contributor

memphys commented Dec 16, 2015

Found same BC break in our project too. After update to 2.8 value attributes just disappeared in the prototype. Anyone can suggest a fast workaround for this without downgrading? I can work on a fix and PR but not too soon.

@HeahDude
Copy link
Contributor

@backbone87 @memphys here's a possible fix :

// Symfony/Component/Form/Extension/Core/CollectionType.php
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        if ($options['allow_add'] && $options['prototype']) {
            $prototype = $builder->create($options['prototype_name'], $options['entry_type'], array_replace(array(
                'label' => $options['prototype_name'].'label__',
            ), $options['entry_options'], ($options['prototype_data'] ? array(
                'data' => $options['prototype_data'],
            ) : array())));
            $builder->setAttribute('prototype', $prototype->getForm());
        }
    ...

@memphys
Copy link
Contributor

memphys commented Dec 16, 2015

@HeahDude thanks, but I already found PR with the original introduction of prototype_data and @backbone87 has already suggested a fix there. Seems like anyway this needs a fix in CollectionType inside Symfony. I'll make a PR tomorrow.

@memphys
Copy link
Contributor

memphys commented Dec 17, 2015

@xabbuh @jakzal @backbone87 made a PR but have some problems with the test. Tested the code in our project and it fixes the BC break. If you can help with some directions on the test, I'll try to finish it. Thanks.

@memphys
Copy link
Contributor

memphys commented Dec 17, 2015

Made new PR to correct branch. Sorry for inconvenience.

@HeahDude
Copy link
Contributor

@memphys you should replace entry_options by options for legacy test

@HeahDude
Copy link
Contributor

I suggest this implementation with a comment for depreciation :

// Symfony/Component/Form/Extension/Core/CollectionType.php
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        if ($options['allow_add'] && $options['prototype']) {
            $prototype = $builder->create($options['prototype_name'], $options['entry_type'], array_replace(array(
                'label' => $options['prototype_name'].'label__',
            // check "$options['prototype_data'] for BC. Condition to be removed in 3.0.
            ), $options['entry_options'], ($options['prototype_data'] ? array(
                'data' => $options['prototype_data'],
            ) : array())));
            $builder->setAttribute('prototype', $prototype->getForm());
        }
        ...

@memphys
Copy link
Contributor

memphys commented Dec 17, 2015

@HeahDude so in 3.0 this BC is intended? I mean if no prototype_data is set, then it won't be filled from form data?

@HeahDude
Copy link
Contributor

@memphys I just suppose it does, considering CollectionTypeTest::testPrototypeData() just before your added test, this should be the expected behaviour

@memphys
Copy link
Contributor

memphys commented Dec 17, 2015

@HeahDude ok, understood, thanks. But it sounds strange for me)

fabpot added a commit that referenced this issue Dec 18, 2015
…(memphys)

This PR was squashed before being merged into the 2.8 branch (closes #17044).

Discussion
----------

[Form] fix BC break introduced with prototype_data option

| Q             | A
| ------------- | ---
| Bug fix?      | [yes]
| New feature?  | [no]
| BC breaks?    | [no]
| Deprecations? | [no]
| Tests pass?   | [no]
| Fixed tickets | [#15707]
| License       | MIT
| Doc PR        | []

This fixes the BC break introduced with prototype_data option in collection type. At the moment whether option is set or not it overwrites prototype data but it has different behaviour before and prototype data was taken from the mapped form data/entity.

- [x] make the test work (can't figure yet how to test that prototype without prototype_data option has default values)

Commits
-------

d73485a [Form] fix BC break introduced with prototype_data option
@fabpot fabpot closed this as completed Dec 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants