Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] Add extra data attributes for collections #5145

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
wants to merge 1 commit into from
Closed

[Form] Add extra data attributes for collections #5145

wants to merge 1 commit into from

Conversation

leevigraham
Copy link
Contributor

PR for discussion.

Bug fix: no
Feature addition: yes
Backwards compatibility break: yes (user implementation of javascript)
Symfony2 tests pass: yes

I've added 3 extra data-* attributes to collections in twig templates and renamed the prototype attribute for easier js manipulation.

New attributes:

  • data-collection: This is a collection and all children are form types
  • data-collection-add: Allow adding of new elements
  • data-collection-delete: Allow deletion of elements (new and existing)

Renamed:

  • data-prototype -> data-collection-protoype: Namespace prototype in the collection.

This should make javascript integration much smoother.

Example js:

$('[data-collection]').each(function(){
    var $collection = $(this);
    var count = $collection.children().length;
    var prototypeString = $collection.data('collectionPrototype');
    var $deleteLink = $("<button class='delete'>Remove</button>");
    var $addLink = $("<button class='add'>Add</button>");

    if($collection.data('collectionDelete')) {
        $collection.children().append($deleteLink.clone());
        $collection.on('click', '.delete', function(){
            event.preventDefault();
            $(this).parent().remove();
        })
    }

    if($collection.data('collectionAdd')) {
        $addLink.click(function(event){
            event.preventDefault();
            newPrototypeString = prototypeString.replace(/__name__/g, count);
            $newPrototype = $(newPrototypeString);
            $newPrototype.append($deleteLink.clone());
            $newPrototype.insertBefore($addLink);
            count++;
        });
        $collection.append($addLink);
    }
})

… element.

Renamed prototype to collection prototype
@travisbot
Copy link

This pull request fails (merged bc623c0 into b1618d2).

{% if prototype is defined %}
{% set attr = attr|merge({'data-prototype': form_row(prototype) }) %}
{% set attr = attr|merge({'data-collection-prototype': form_row(prototype)}) %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a BC break as it changes the attribute name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, you're right. I didn't think it was because it's only in the template. What is the correct course of action from here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not only in the template. It is also used by the JS code implemented by users with the existing attribute name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to get a PR like this merged if it breaks backwards compatibility? IMO the break is pretty small. I could resubmit with the additions only but then the attributes would be inconsistent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I think using data-prototype as name would be better as it would keep the BC.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that data-collection-prototype would be a better name here, but I don't want to break BC. Can you please revert this and add a TODO-3.0.md file in the symfony root directory where you note that this attribute should be renamed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bschussek Just to be 100% clear.

  • Keep the new attributes: data-collection, data-collection-add & data-collection-delete
  • Revert data-collection-prototype to data-prototype
  • Add data-collection -> data-collection-prototype to TODO-3.0.md

👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm against adding a TODO-3.0.md in the repository. Instead, create an issue on Github.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leevigraham exactly

@stof
Copy link
Member

stof commented Oct 13, 2012

@fabpot what do you think about it ?

@leevigraham
Copy link
Contributor Author

Closed in favour of #7713

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants