diff --git a/.gitattributes b/.gitattributes index b6ae860..bdd1ba9 100644 --- a/.gitattributes +++ b/.gitattributes @@ -7,3 +7,4 @@ /.coveralls.yml export-ignore /.php_cs export-ignore /phpunit.xml.dist export-ignore +/images export-ignore diff --git a/README.md b/README.md index 49b4945..382e7e5 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,11 @@ UX collection JS is a Symfony bundle providing Symfony UX integration for collection form type with the help from [Symfony Collection JS](https://github.com/ruano-a/symfonyCollectionJs) library. +## Screenshots + +![Screenshot Bootstrap 5](./images/collection-js-bootstrap-5.png) +![Screenshot EasyAdmin](./images/collection-js-easyadmin.png) + ## Installation UX Collection JS requires PHP 7.4+ and Symfony 4.4+. @@ -100,7 +105,14 @@ class FormFieldReferenceController extends AbstractCrudController { yield CollectionField::new('collectionSimple', 'Collection Field (simple)') ->setFormType(CollectionJsType::class) - ->setFormTypeOption('entry_type', CollectionSimpleType::class) + ->setFormTypeOptions([ + 'entry_type' => CollectionSimpleType::class, + 'allow_add' => true, + 'allow_delete' => true, + 'allow_move_up' => true, + 'allow_move_down' => true, + 'call_post_add_on_init' => true, + ]) ->addWebpackEncoreEntries('stimulus'); } } diff --git a/images/collection-js-bootstrap-5.png b/images/collection-js-bootstrap-5.png new file mode 100644 index 0000000..82525db Binary files /dev/null and b/images/collection-js-bootstrap-5.png differ diff --git a/images/collection-js-easyadmin.png b/images/collection-js-easyadmin.png new file mode 100644 index 0000000..1543b4a Binary files /dev/null and b/images/collection-js-easyadmin.png differ diff --git a/src/Form/CollectionJsType.php b/src/Form/CollectionJsType.php index 558e315..8155c91 100644 --- a/src/Form/CollectionJsType.php +++ b/src/Form/CollectionJsType.php @@ -6,6 +6,7 @@ use Symfony\Component\Form\Extension\Core\Type\CollectionType; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormView; +use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; class CollectionJsType extends AbstractType @@ -23,18 +24,18 @@ public function getParent(): string */ public function configureOptions(OptionsResolver $resolver): void { + $entryOptionsNormalizer = function (Options $options, $value) { + $value['block_prefix'] = $value['block_prefix'] ?? 'collection_js_entry'; + + return $value; + }; + $resolver->setDefaults([ 'allow_move_up' => false, 'allow_move_down' => false, - 'entry_options' => function (OptionsResolver $entryOptionsResolver) { - $entryOptionsResolver - ->setDefaults([ - 'label' => false, - 'block_prefix' => 'collection_js_entry', - ]); - }, 'call_post_add_on_init' => false, ]); + $resolver->setNormalizer('entry_options', $entryOptionsNormalizer); } /** diff --git a/src/Resources/views/form_div_layout.html.twig b/src/Resources/views/form_div_layout.html.twig index e4b5555..3dbcb20 100644 --- a/src/Resources/views/form_div_layout.html.twig +++ b/src/Resources/views/form_div_layout.html.twig @@ -82,7 +82,11 @@ {% endblock collection_js_actions %} {% block collection_js_accordion_item_body %} - {% for child in form|filter(child => not child.rendered) %} - {{- form_row(child) -}} - {% endfor %} + {% if form|length > 0 %} + {% for child in form|filter(child => not child.rendered) %} + {{- form_row(child) -}} + {% endfor %} + {% else %} + {{ form_widget(form) }} + {% endif %} {% endblock collection_js_accordion_item_body %} diff --git a/tests/Form/CollectionJsTypeTest.php b/tests/Form/CollectionJsTypeTest.php index cf1f707..10ae012 100644 --- a/tests/Form/CollectionJsTypeTest.php +++ b/tests/Form/CollectionJsTypeTest.php @@ -14,7 +14,6 @@ class CollectionJsTypeTest extends TypeTestCase public function testDefaultOptions() { $entryOptions = [ - 'label' => true, 'block_prefix' => 'custom_collection_js_entry', ]; $form = $this->factory @@ -23,9 +22,7 @@ public function testDefaultOptions() 'entry_options' => $entryOptions, ]) ; - $this->assertSame($entryOptions + [ - 'block_name' => 'entry', - ], $form->getConfig()->getOption('entry_options')); + $this->assertSame($entryOptions, $form->getConfig()->getOption('entry_options')); $view = $form->createView(); $this->assertFalse($view->vars['allow_move_up']); @@ -46,9 +43,7 @@ public function testCustomOptions() ]) ; $this->assertSame([ - 'label' => false, 'block_prefix' => 'collection_js_entry', - 'block_name' => 'entry', ], $form->getConfig()->getOption('entry_options')); $view = $form->createView();