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

Skip to content

[Form] Added additional data attributes for collections #7713

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 2 commits into from
Closed

[Form] Added additional data attributes for collections #7713

wants to merge 2 commits into from

Conversation

leevigraham
Copy link
Contributor

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets na
License MIT
Doc PR WIP
  • fix the tests as they have not been updated yet
  • submit changes to the documentation

I've added 4 extra data-* attributes to collections in twig and php views for easier js manipulation.

New attributes:

  • data-form-widget='collection': This is a collection and all children are form types
  • data-collection-allow-add: Allow adding of new elements
  • data-collection-allow-delete: Allow deletion of elements (new and existing)
  • data-prototype-name: The prototype name

This should make javascript integration much easier.

Example js:

$('[data-form-widget=collection]').each(function(){

    var $collection = $(this),
        prototypeString = $collection.data('prototype'),
        prototypeName = $collection.data('prototypeName') || '__name__',
        prototypeNameRegx = new RegExp(prototypeName+"(label__)?","g"),
        count = $collection.children().length;

    var $deleteLink = $("<span />", {
        'class': 'js-delete',
        'text': 'Delete'
    });

    var $addLink = $("<span />", {
        'class': 'js-add',
        'text': 'Add'
    });

    if($collection.data('collectionAllowDelete')) {

        $collection
            .children()
            .append($deleteLink.clone(true));

        $collection.on('click', '.js-delete', function(){
            event.preventDefault();
            $(this).parent().remove();
        });
    }

    if($collection.data('collectionAllowAdd')) {

        $addLink.click(function(event){
            event.preventDefault();
            var newPrototypeString = prototypeString.replace(prototypeNameRegx, count),
                $newPrototype = $(newPrototypeString).append($deleteLink.clone(true));
            $newPrototype.insertBefore($addLink);
            count++;
        });

        $collection.append($addLink);
    }
});

Replaces #5145

@stof
Copy link
Member

stof commented Apr 18, 2013

You should also add an attribute with the prototype name. Otherwise, you have to hardcode the prototype name in the JS and it breaks when using a different value (which is needed for nested collections)

{% if prototype is defined %}
{% set attr = attr|merge({'data-prototype': form_row(prototype) }) %}
{% endif %}

{% if allow_delete is defined %}
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 always defined. But it may be false

@stof
Copy link
Member

stof commented Apr 18, 2013

you should update the rendering testsuite for these attributes, with cases testing that they are added when needed and also cases testing that they are not added when they should not. I would show you that the conditions are currently wrong and that you forgot updating the PHP templates

@leevigraham
Copy link
Contributor Author

Will do and resubmit.

@webmozart
Copy link
Contributor

@leevigraham It is sufficient to push your branch again (with the -f switch if you amend commits), then the PR will be updated without having to create a new one.

@leevigraham
Copy link
Contributor Author

@bschussek I think I deleted my old fork and local repo hence the new request. I'll push back up to this same PR tomorrow.

@stof
Copy link
Member

stof commented Apr 22, 2013

@leevigraham ping

@stof
Copy link
Member

stof commented Apr 24, 2013

@leevigraham could you finish it ?

@leevigraham
Copy link
Contributor Author

I'll try and work on this tonight when I get home from the office. :)

On 24/04/2013, at 7:10 PM, Christophe Coevoet [email protected] wrote:

@leevigraham could you finish it ?


Reply to this email directly or view it on GitHub.

@leevigraham
Copy link
Contributor Author

@stof How would you feel about adding data-collection-allow-delete & data-collection-allow-add vs data-collection-delete & data-collection-add

@fabpot
Copy link
Member

fabpot commented Apr 25, 2013

@leevigraham Can you submit a PR on the docs?

@@ -1747,7 +1751,31 @@ public function testCollectionPrototype()
$this->assertMatchesXpath($html,
'//div[@id="name_items"][@data-prototype]
|
//table[@id="name_items"][@data-prototype]'
//table[@id="name_items"][@data-prototype]'
);
Copy link
Contributor

Choose a reason for hiding this comment

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

This and the following tests should be split across AbstractDivLayoutTest and AbstractTableLayoutTest. We want to test that div layouts contain no tables and vice versa.

Copy link
Contributor

Choose a reason for hiding this comment

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

The tests should also be more specific, i.e. "./table" instead of "//table".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To easy. I copied the existing test but it's not hard to split. Should there be a separate test for each attribute?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not necessarily, you can test all of them in one XPath. :)

* Added additional data attributes for collections:
    * data-form-widget="collection" - defines that the field is a collection
    * data-collection-allow-delete - defines collection items can be deleted
    * data-collection-allow-add - defines collection items can be added
* Added `prototype_name` as view variable rather than accessing prototype.vars.name
* Updated .twig and php templates
@@ -1735,19 +1735,40 @@ public function testUrl()
);
}

public function testCollection()
Copy link
Contributor

Choose a reason for hiding this comment

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

This test doesn't really serve any purpose, since it is overridden in the subclasses anyway.

@stof
Copy link
Member

stof commented Sep 17, 2013

@fabpot @bschussek is there any chance to have this merged for 2.4 ?

{% set attr = attr|merge({
'data-form-widget': 'collection',
'data-collection-allow-add': allow_add,
'data-collection-allow-delete': allow_delete,
Copy link
Member

Choose a reason for hiding this comment

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

casting a boolean to a string will display 1 or an empty string. I don't think it is the best stuff we can pass to the JS

@jakzal
Copy link
Contributor

jakzal commented Dec 23, 2013

@leevigraham have you got time to finish this one off?

@leevigraham
Copy link
Contributor Author

Closing in favour of a refactored and up to date w/ master: Pull Request #10071

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

Successfully merging this pull request may close these issues.

5 participants