Navigation Menu

Skip to content

Commit

Permalink
[Form] Changed collection prototype rendering.
Browse files Browse the repository at this point in the history
Based on PR 1500. It is now rendered inside an attribute of collection
tag.
  • Loading branch information
marc.weistroff committed Jul 22, 2011
1 parent 49dda53 commit 2b4cc9b
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/Extension/FormExtension.php
Expand Up @@ -223,7 +223,7 @@ protected function render(FormView $view, $section, array $variables = array())
}
}

$custom = '_'.$view->get('proto_id', $view->get('id'));
$custom = '_'.$view->get('id');
$rendering = $custom.$section;
$blocks = $this->getBlocks($view);

Expand Down
Expand Up @@ -9,6 +9,15 @@
{% endspaceless %}
{% endblock form_widget %}

{% block collection_widget %}
{% spaceless %}
{% if prototype is defined %}
{% set attr = attr|merge({'data-prototype': form_row(prototype) }) %}
{% endif %}
{{ block('form_widget') }}
{% endspaceless %}
{% endblock collection_widget %}

{% block textarea_widget %}
{% spaceless %}
<textarea {{ block('widget_attributes') }}>{{ value }}</textarea>
Expand Down Expand Up @@ -222,12 +231,6 @@
{% endspaceless %}
{% endblock field_row %}

{% block prototype_row %}
{% spaceless %}
<script type="text/html" id="{{ proto_id }}">{{ form_row(form) }}</script>
{% endspaceless %}
{% endblock prototype_row %}

{% block hidden_row %}
{{ form_widget(form) }}
{% endblock hidden_row %}
Expand Down
@@ -0,0 +1,4 @@
<?php if (isset($prototype)): ?>
<?php $attr['data-prototype'] = $view->escape($view['form']->row($prototype)) ?>
<?php endif ?>
<?php echo $view['form']->widget($form, array('attr' => $attr)) ?>

This file was deleted.

Expand Up @@ -199,7 +199,7 @@ protected function renderSection(FormView $view, $section, array $variables = ar

$template = null;

$custom = '_'.$view->get('proto_id', $view->get('id'));
$custom = '_'.$view->get('id');
$rendering = $custom.$section;

if (isset($this->varStack[$rendering])) {
Expand Down
Expand Up @@ -27,7 +27,8 @@ public function buildForm(FormBuilder $builder, array $options)
{
if ($options['allow_add'] && $options['prototype']) {
$prototype = $builder->create('$$name$$', $options['type'], $options['options']);
$builder->setAttribute('prototype', $prototype);
$builder->setAttribute('prototype', $prototype->getForm());
$builder->add($prototype);
}

$dataClass = isset($options['options']['data_class']) ? $options['options']['data_class'] : null;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function buildView(FormView $view, FormInterface $form)
;

if ($form->hasAttribute('prototype')) {
$view->set('prototype', $form->getAttribute('prototype'));
$view->set('prototype', $form->getAttribute('prototype')->createView($view));
}
}

Expand Down
10 changes: 0 additions & 10 deletions src/Symfony/Component/Form/Form.php
Expand Up @@ -925,16 +925,6 @@ public function createView(FormView $parent = null)
$childViews[$key] = $child->createView($view);
}

if (null !== $prototype = $view->get('prototype')) {
$protoView = $prototype->getForm()->createView($view);
$protoTypes = $protoView->get('types');
$protoTypes[] = 'prototype';
$childViews[$prototype->getName()] = $protoView
->set('types', $protoTypes)
->set('proto_id', $view->get('id').'_prototype');
;
}

$view->setChildren($childViews);

foreach ($types as $type) {
Expand Down
7 changes: 4 additions & 3 deletions tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php
Expand Up @@ -1692,9 +1692,10 @@ public function testCollectionPrototype()
$html = $this->renderWidget($form);

$this->assertMatchesXpath($html,
'//script
[@id="na&me_items_prototype"]
[@type="text/html"]
'//div[@id="na&me_items"][@data-prototype]
|
//table[@id="na&me_items"][@data-prototype]
'
);
}
Expand Down

0 comments on commit 2b4cc9b

Please sign in to comment.