Skip to content

Commit

Permalink
Fix variable 'prototype' does not exist on 'allow_add' = false
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Feb 7, 2022
1 parent 38b87d9 commit 9f80efe
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class PostType extends AbstractType
{
$builder
// ...
->add('attachments', CollectionJsType::class, [
'entry_type' => FileType::class,
->add('authors', CollectionJsType::class, [
'entry_type' => TextType::class,
'allow_add' => true,
'allow_delete' => true,
'allow_move_up' => true,
Expand Down
19 changes: 19 additions & 0 deletions src/Form/CollectionJsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
Expand All @@ -19,6 +20,24 @@ public function getParent(): string
return CollectionType::class;
}

/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$prototypeOptions = array_replace([
'required' => $options['required'],
'label' => $options['prototype_name'].'label__',
], $options['entry_options']);

if (null !== $options['prototype_data']) {
$prototypeOptions['data'] = $options['prototype_data'];
}

$prototype = $builder->create($options['prototype_name'], $options['entry_type'], $prototypeOptions);
$builder->setAttribute('prototype', $prototype->getForm());
}

/**
* {@inheritdoc}
*/
Expand Down
8 changes: 1 addition & 7 deletions src/Resources/views/bootstrap_3_layout.html.twig
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
{% use "@CollectionJs/bootstrap_base_layout.html.twig" %}

{% block collection_js_accordion %}
<div class="panel-group collection-js-root" data-prototype="{{ form_widget(prototype) | e }}" role="tablist" aria-multiselectable="true">
{% for child in form|filter(child => not child.rendered) %}
{{- form_widget(child) -}}
{% endfor %}
</div>
{% endblock collection_js_accordion %}
{% block collection_js_accordion_class %}panel-group{% endblock collection_js_accordion_class %}

{% block collection_js_accordion_item %}
<div class="panel panel-default">
Expand Down
8 changes: 0 additions & 8 deletions src/Resources/views/bootstrap_4_layout.html.twig
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
{% use "@CollectionJs/bootstrap_base_layout.html.twig" %}

{% block collection_js_accordion %}
<div class="collection-js-root" data-prototype="{{ form_widget(prototype) | e }}">
{% for child in form|filter(child => not child.rendered) %}
{{- form_widget(child) -}}
{% endfor %}
</div>
{% endblock collection_js_accordion %}

{% block collection_js_accordion_item %}
<div class="card">
<div class="card-header">
Expand Down
8 changes: 1 addition & 7 deletions src/Resources/views/bootstrap_5_layout.html.twig
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
{% use "@CollectionJs/bootstrap_base_layout.html.twig" %}

{% block collection_js_accordion %}
<div class="accordion collection-js-root" data-prototype="{{ form_widget(prototype) | e }}">
{% for child in form|filter(child => not child.rendered) %}
{{- form_widget(child) -}}
{% endfor %}
</div>
{% endblock collection_js_accordion %}
{% block collection_js_accordion_class %}accordion{% endblock collection_js_accordion_class %}

{% block collection_js_accordion_item %}
<div class="accordion-item">
Expand Down
4 changes: 3 additions & 1 deletion src/Resources/views/form_div_layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
{% endblock collection_js_add_button %}

{% block collection_js_accordion %}
<div class="collection-js-root" data-prototype="{{ form_widget(prototype) | e }}">
<div class="{{ block('collection_js_accordion_class') }} collection-js-root" data-prototype="{{ form_widget(prototype) | e }}">
{% for child in form|filter(child => not child.rendered) %}
{{- form_widget(child) -}}
{% endfor %}
</div>
{% endblock collection_js_accordion %}

{% block collection_js_accordion_class %}{% endblock collection_js_accordion_class %}

{% block collection_js_entry_widget %}
{{ block('collection_js_accordion_item') }}
{% endblock collection_js_entry_widget %}
Expand Down

0 comments on commit 9f80efe

Please sign in to comment.