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
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,10 +31,22 @@

{% block collection_widget %}
{% spaceless %}

{% set attr = attr|merge({
'data-form-widget': 'collection',
Copy link
Contributor

Choose a reason for hiding this comment

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

Why "data-form-widget" and not "data-form-type"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking form-widget could eventually be applied to all form rendering. If that was the case for the date form type you would have multiple widgets. Given that any js functionality is widget specific (choice, text, single text) rather than type I thought data-form-widget was more specific.

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 Are you ok with the reasoning for this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ping @bschussek

Copy link
Contributor

Choose a reason for hiding this comment

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

Fine by me! :)

'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

}) %}

{% 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 %}

Copy link
Contributor

Choose a reason for hiding this comment

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

Do the empty lines and line breaks introduced here appear in the final HTML?

Copy link
Member

Choose a reason for hiding this comment

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

they will be removed by the {% spaceless %} tag as they will be between HTML tags

{{ block('form_widget') }}

{% endspaceless %}
{% endblock collection_widget %}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?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)) ?>
<?php echo $view['form']->widget($form, array('attr' => $attr)) ?>
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
));

if ($form->getConfig()->hasAttribute('prototype')) {
$view->vars['prototype_name'] = $options['prototype_name'];
$view->vars['prototype'] = $form->getConfig()->getAttribute('prototype')->createView($view);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,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 Down Expand Up @@ -331,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 Down
29 changes: 25 additions & 4 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

{
$form = $this->factory->createNamed('name', 'collection', array('a', 'b'), array(
'type' => 'text',
));

$html = $this->renderWidget($form);
$this->assertWidgetMatchesXpath($html, array(),
'//div[@data-form-widget="collection"]'
);
}

public function testCollectionPrototype()
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 should be moved to the AbstractDivLayoutTest

{
$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]
|
//table[@id="name_items"][@data-prototype]'
'//div
[@data-form-widget="collection"]
[@id="name_items"]
[@data-prototype-name="__item__"]
[@data-prototype]
[@data-collection-allow-add="1"]
[@data-collection-allow-delete="1"]
'
);
}

Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,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 @@ -217,12 +218,37 @@ 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]
'
);
}

public function testCollectionPrototype()
{
$form = $this->factory->createNamedBuilder('name', 'form', array('items' => array('one', 'two', 'three')))
->add('items', 'collection', array(
'allow_add' => true,
'allow_delete' => true,
'prototype_name' => '__item__'
))
->getForm()
->createView();

$html = $this->renderWidget($form);

$this->assertMatchesXpath($html,
'//table[@id="name_items"]
[@data-form-widget="collection"]
[@id="name_items"]
[@data-prototype-name="__item__"]
[@data-prototype]
[@data-collection-allow-add="1"]
[@data-collection-allow-delete="1"]'
);
}

public function testForm()
{
$view = $this->factory->createNamedBuilder('name', 'form')
Expand Down