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

Skip to content

[Form] Added additional data attributes for collection type output #10071

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?php $attr['data-form-widget'] = 'collection' ?>
<?php $attr['data-collection-allow-add'] = $allow_add ?>
<?php $attr['data-collection-allow-delete'] = $allow_delete ?>
<?php if (isset($prototype)): ?>
<?php $attr['data-prototype-name'] = $prototype_name; ?>
<?php $attr['data-prototype'] = $view->escape($view['form']->row($prototype)) ?>
<?php endif ?>
<?php echo $view['form']->widget($form, array('attr' => $attr)) ?>
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"]]
Expand All @@ -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"]]
Expand All @@ -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]
'
Expand All @@ -351,6 +354,7 @@ public function testCollectionRow()

$this->assertWidgetMatchesXpath($form->createView(), array(),
'/div
[@data-form-widget="collection"]
[
./div
[
Expand Down Expand Up @@ -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]"]]
Expand Down
23 changes: 20 additions & 3 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
'
);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"]]
Expand All @@ -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]
'
Expand Down Expand Up @@ -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]"]]
Expand Down