From 16b300c0ed1e6d61426fc961b68c6f3fefdcd5a0 Mon Sep 17 00:00:00 2001 From: Leevi Graham Date: Mon, 20 Jan 2014 00:05:18 +1100 Subject: [PATCH] Added additional data attributes for collection type output --- .../views/Form/form_div_layout.html.twig | 10 +++++++- .../views/Form/collection_widget.html.php | 4 ++++ .../Extension/Core/Type/CollectionType.php | 1 + .../Form/Tests/AbstractDivLayoutTest.php | 5 ++++ .../Form/Tests/AbstractLayoutTest.php | 23 ++++++++++++++++--- .../Form/Tests/AbstractTableLayoutTest.php | 3 +++ 6 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig b/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig index 7f5e323975478..d9ee563cd420a 100644 --- a/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig +++ b/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig @@ -31,8 +31,16 @@ {% block collection_widget %} {% spaceless %} + {% set attr = attr|merge({ + 'data-form-widget': 'collection', + 'data-collection-allow-add': allow_add, + 'data-collection-allow-delete': allow_delete, + }) %} {% if prototype is defined %} - {% set attr = attr|merge({'data-prototype': form_row(prototype) }) %} + {% set attr = attr|merge({ + 'data-prototype-name': prototype_name, + 'data-prototype': form_row(prototype) + }) %} {% endif %} {{ block('form_widget') }} {% endspaceless %} diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/collection_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/collection_widget.html.php index 9f6a94bcefc93..d37cee52c0433 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/collection_widget.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/collection_widget.html.php @@ -1,4 +1,8 @@ + + + + escape($view['form']->row($prototype)) ?> widget($form, array('attr' => $attr)) ?> diff --git a/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php b/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php index d039f98d6810e..80ffe8a80803d 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php @@ -56,6 +56,7 @@ public function buildView(FormView $view, FormInterface $form, array $options) if ($form->getConfig()->hasAttribute('prototype')) { $view->vars['prototype'] = $form->getConfig()->getAttribute('prototype')->createView($view); + $view->vars['prototype_name'] = $options['prototype_name']; } } diff --git a/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php index 2d32aa38a398b..9a483ac6e83ed 100644 --- a/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php @@ -290,6 +290,7 @@ public function testCollection() $this->assertWidgetMatchesXpath($form->createView(), array(), '/div + [@data-form-widget="collection"] [ ./div[./input[@type="text"][@value="a"]] /following-sibling::div[./input[@type="text"][@value="b"]] @@ -312,6 +313,7 @@ public function testCollectionWithAlternatingRowTypes() $this->assertWidgetMatchesXpath($form->createView(), array(), '/div + [@data-form-widget="collection"] [ ./div[./div/div/input[@type="text"][@value="a"]] /following-sibling::div[./div/div/textarea[.="b"]] @@ -330,6 +332,7 @@ public function testEmptyCollection() $this->assertWidgetMatchesXpath($form->createView(), array(), '/div + [@data-form-widget="collection"] [./input[@type="hidden"][@id="name__token"]] [count(./div)=0] ' @@ -351,6 +354,7 @@ public function testCollectionRow() $this->assertWidgetMatchesXpath($form->createView(), array(), '/div + [@data-form-widget="collection"] [ ./div [ @@ -676,6 +680,7 @@ public function testCollectionRowWithCustomBlock() $this->assertWidgetMatchesXpath($form->createView(), array(), '/div + [@data-form-widget="collection"] [ ./div[./label[.="Custom label: [trans]0[/trans]"]] /following-sibling::div[./label[.="Custom label: [trans]1[/trans]"]] diff --git a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php index 28827e51b75c8..89735b48e8b20 100644 --- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php @@ -1760,16 +1760,33 @@ public function testUrl() public function testCollectionPrototype() { $form = $this->factory->createNamedBuilder('name', 'form', array('items' => array('one', 'two', 'three'))) - ->add('items', 'collection', array('allow_add' => true)) + ->add('items', 'collection', array( + 'allow_add' => true, + 'allow_delete' => true, + 'prototype_name' => '__item__' + )) ->getForm() ->createView(); $html = $this->renderWidget($form); $this->assertMatchesXpath($html, - '//div[@id="name_items"][@data-prototype] + '//div + [@id="name_items"] + [@data-form-widget="collection"] + [@data-prototype] + [@data-prototype-name="__item__"] + [@data-prototype-allow-add="data-collection-allow-add"] + [@data-prototype-allow-add="data-collection-allow-delete"] | - //table[@id="name_items"][@data-prototype]' + //table + [@id="name_items"] + [@data-form-widget="collection"] + [@data-prototype] + [@data-prototype-name="__item__"] + [@data-prototype-allow-add="data-collection-allow-add"] + [@data-prototype-allow-add="data-collection-allow-delete"] + ' ); } diff --git a/src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php index 5c8a9be2d5959..74dfc29f0162d 100644 --- a/src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php @@ -200,6 +200,7 @@ public function testCollection() $this->assertWidgetMatchesXpath($form->createView(), array(), '/table + [@data-form-widget="collection"] [ ./tr[./td/input[@type="text"][@value="a"]] /following-sibling::tr[./td/input[@type="text"][@value="b"]] @@ -218,6 +219,7 @@ public function testEmptyCollection() $this->assertWidgetMatchesXpath($form->createView(), array(), '/table + [@data-form-widget="collection"] [./tr[@style="display: none"][./td[@colspan="2"]/input[@type="hidden"][@id="name__token"]]] [count(./tr[./td/input])=1] ' @@ -445,6 +447,7 @@ public function testCollectionRowWithCustomBlock() $this->assertWidgetMatchesXpath($form->createView(), array(), '/table + [@data-form-widget="collection"] [ ./tr[./td/label[.="Custom label: [trans]0[/trans]"]] /following-sibling::tr[./td/label[.="Custom label: [trans]1[/trans]"]]