Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
dbba3b8
UX FormCollection
stakovicz May 3, 2021
83ec2ea
Remove hard coded property_name __name__
stakovicz May 4, 2021
d983cf0
PHP CS Fixer
stakovicz May 4, 2021
a35ff32
First jests
stakovicz May 4, 2021
e1d7574
Rename CollectionType > UXCollectionType
stakovicz May 6, 2021
409c8e7
Rename CollectionType > UXCollectionType
stakovicz May 6, 2021
a624f25
DependencyInjection Clean
stakovicz May 6, 2021
f4ba011
Fix .gitattributes
stakovicz May 6, 2021
7741907
Move default values
stakovicz May 7, 2021
66defab
Predefined theme or not
stakovicz May 23, 2021
145b1cb
Update src/FormCollection/README.md
stakovicz May 24, 2021
2b900cc
Update src/FormCollection/README.md
stakovicz May 24, 2021
25c1454
Update src/FormCollection/README.md
stakovicz May 24, 2021
d66250d
Update src/FormCollection/Resources/views/form_theme_div.html.twig
stakovicz May 24, 2021
2db95e0
Update src/FormCollection/README.md
stakovicz May 24, 2021
8f8b5c2
Update src/FormCollection/Resources/views/form_theme_table.html.twig
stakovicz May 24, 2021
4a68faa
Split in 4 options
stakovicz May 24, 2021
a170618
Default startIndex value
stakovicz Jun 6, 2021
13561e7
Update src/FormCollection/Resources/views/form_theme_div.html.twig
stakovicz Jul 21, 2021
fb847c6
Update src/FormCollection/Resources/views/form_theme_div.html.twig
stakovicz Jul 21, 2021
740d19b
Update src/FormCollection/Resources/views/form_theme_table.html.twig
stakovicz Jul 21, 2021
c57d86e
Update src/FormCollection/Resources/views/form_theme_table.html.twig
stakovicz Jul 21, 2021
0bbac2f
Fix coding-style-js
stakovicz Nov 6, 2021
edfe323
Prettier
stakovicz Nov 6, 2021
086c3a3
Rebase and refresh the code
stakovicz May 21, 2022
480dbb5
fix TU
stakovicz May 21, 2022
3223fae
change buttons attr
stakovicz May 21, 2022
911f18f
Move logic from twig files to form types
alexander-schranz Jul 18, 2022
4cfa27d
Fix types and not used imports
alexander-schranz Jul 18, 2022
0976a9e
Allow overriding the data-controller via attr
alexander-schranz Jul 18, 2022
dc18974
Simplify controller integration
alexander-schranz Jul 18, 2022
9ee00dc
Fix handling of nested blocks
alexander-schranz Jul 18, 2022
d294592
Move docs to index.rst
alexander-schranz Jul 18, 2022
4edfefb
Fix add problem
alexander-schranz Jul 19, 2022
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
Prev Previous commit
Next Next commit
Move docs to index.rst
  • Loading branch information
alexander-schranz committed Jul 18, 2022
commit d29459272bdf08543e40f2f25ed337764ecba368
178 changes: 10 additions & 168 deletions src/FormCollection/README.md
Original file line number Diff line number Diff line change
@@ -1,172 +1,14 @@
# UX Form Collection
# Symfony UX Form Collection

Symfony UX Form collection is a Symfony bundle providing light UX for collection
in Symfony Forms.
Symfony UX Form collection is a Symfony bundle provides a Stimulus integration
for add and remove of for [Symfony Form Collection Type](https://symfony.com/doc/current/form/collection.html).

## Installation
**This repository is a READ-ONLY sub-tree split**. See
https://github.com/symfony/ux to create issues or submit pull requests.

UX Form Collection requires PHP 7.2+ and Symfony 4.4+.
## Resources

Install this bundle using Composer and Symfony Flex:

```sh
composer require symfony/ux-form-collection

# Don't forget to install the JavaScript dependencies as well and compile
yarn install --force
yarn encore dev
```

Also make sure you have at least version 2.0 of [@symfony/stimulus-bridge](https://github.com/symfony/stimulus-bridge)
in your `package.json` file.

## Usage

The most common usage of Form Collection is to use it as a replacement of
the native CollectionType class:

```php
// ...
use Symfony\UX\FormCollection\Form\UXCollectionType;

class BlogFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('comments', UXCollectionType::class, [
'label' => 'Comments',
'ux_entry_type' => CommentType::class,
'entry_options' => [
'label' => false,
],
'allow_add' => true,
'allow_delete' => true,
'add_options' => [
'label' => 'Add comment',
],
'delete_options' => [
'label' => 'Remove Comment',
],
]);
}

// ...
}
```

You can display it using Twig as you would normally with any form:

```twig
{{ form(form) }}
```

## Theming

### Change position of the entry toolbar

```twig
{%- block ux_collection_entry_widget -%}
{%- set toolbar -%}
{{- form_widget(form.toolbar) -}}
{%- endset -%}

<div class="mt-4 bg-gray-50 border shadow p-4">
{{- toolbar -}}

{{- block('form_rows') -}}

<div class="mt-4 flex justify-end flex-wrap">
{{- toolbar -}}
</div>
</div>
{%- endblock -%}
```

### Change entry toolbar

```twig
{%- block ux_collection_entry_toolbar_widget -%}
<div class="mt-4 flex justify-end flex-wrap">
{{- block('form_widget') -}}
</div>
{%- endblock -%}
```

### Change collection toolbar

```twig
{%- block ux_collection_toolbar_widget -%}
<div class="mt-4 flex justify-center flex-wrap">
{{- block('form_widget') -}}
</div>
{%- endblock -%}
```

### Extend the default behavior

Symfony UX Form Collection allows you to extend its default behavior using a custom Stimulus controller:

```js
// mycollection_controller.js

import { Controller } from 'stimulus';

export default class extends Controller {
connect() {
this.element.addEventListener('collection:pre-connect', this._onPreConnect);
this.element.addEventListener('collection:connect', this._onConnect);
this.element.addEventListener('collection:pre-add', this._onPreAdd);
this.element.addEventListener('collection:add', this._onAdd);
this.element.addEventListener('collection:pre-delete', this._onPreDelete);
this.element.addEventListener('collection:delete', this._onDelete);
}

disconnect() {
// You should always remove listeners when the controller is disconnected to avoid side effects
this.element.removeEventListener('collection:pre-connect', this._onPreConnect);
this.element.removeEventListener('collection:connect', this._onConnect);
}

_onPreConnect(event) {
// The collection is not yet connected
console.log(event.detail.allowAdd); // Access to the allow_add option of the form
console.log(event.detail.allowDelete); // Access to the allow_delete option of the form
}

_onConnect(event) {
// Same as collection:pre-connect event
}

_onPreAdd(event) {
console.log(event.detail.index); // Access to the curent index will be added
console.log(event.detail.element); // Access to the element will be added
}

_onAdd(event) {
// Same as collection:pre-add event
}

_onPreDelete(event) {
console.log(event.detail.index); // Access to the index will be removed
console.log(event.detail.element); // Access to the elemnt will be removed
}

_onDelete(event) {
// Same as collection:pre-delete event
}
}
```

Then in your render call, add your controller as an HTML attribute:

```php
$builder
// ...
->add('comments', UXCollectionType::class, [
// ...
'attr' => [
// Change the controller name
'data-controller' => 'mycollection'
]
]);
```
- [Documentation](https://symfony.com/bundles/ux-form-collection/current/index.html)
- [Report issues](https://github.com/symfony/ux/issues) and
[send Pull Requests](https://github.com/symfony/ux/pulls)
in the [main Symfony UX repository](https://github.com/symfony/ux)
180 changes: 180 additions & 0 deletions src/FormCollection/Resources/doc/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
UX Form Collection
===================

Symfony UX Form collection is a Symfony bundle providing light UX for collection
in Symfony Forms.

Installation
------------

UX Form Collection requires PHP 7.2+ and Symfony 4.4+.

Install this bundle using Composer and Symfony Flex:

.. code-block:: sh

composer require symfony/ux-form-collection

# Don't forget to install the JavaScript dependencies as well and compile
yarn install --force
yarn encore dev

Also make sure you have at least version 2.0 of [@symfony/stimulus-bridge](https://github.com/symfony/stimulus-bridge)
in your `package.json` file.

Usage
-----

The most common usage of Form Collection is to use it as a replacement of
the native CollectionType class:

.. code-block:: php

// ...
use Symfony\UX\FormCollection\Form\UXCollectionType;

class BlogFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('comments', UXCollectionType::class, [
'label' => 'Comments',
'ux_entry_type' => CommentType::class,
'entry_options' => [
'label' => false,
],
'allow_add' => true,
'allow_delete' => true,
'add_options' => [
'label' => 'Add comment',
],
'delete_options' => [
'label' => 'Remove Comment',
],
]);
}

// ...
}

You can display it using Twig as you would normally with any form:

.. code-block:: twig

{{ form(form) }}


Theming
-------

Change position of the entry toolbar
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: twig

{%- block ux_collection_entry_widget -%}
{%- set toolbar -%}
{{- form_widget(form.toolbar) -}}
{%- endset -%}

<div class="mt-4 bg-gray-50 border shadow p-4">
{{- toolbar -}}

{{- block('form_rows') -}}

<div class="mt-4 flex justify-end flex-wrap">
{{- toolbar -}}
</div>
</div>
{%- endblock -%}

Change entry toolbar
~~~~~~~~~~~~~~~~~~~~

.. code-block:: twig
{%- block ux_collection_entry_toolbar_widget -%}
<div class="mt-4 flex justify-end flex-wrap">
{{- block('form_widget') -}}
</div>
{%- endblock -%}

Change collection toolbar
~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: twig

{%- block ux_collection_toolbar_widget -%}
<div class="mt-4 flex justify-center flex-wrap">
{{- block('form_widget') -}}
</div>
{%- endblock -%}

Extend the default behavior
---------------------------

Symfony UX Form Collection allows you to extend its default behavior using a custom Stimulus controller:

.. code-block:: js

// mycollection_controller.js

import { Controller } from 'stimulus';

export default class extends Controller {
connect() {
this.element.addEventListener('collection:pre-connect', this._onPreConnect);
this.element.addEventListener('collection:connect', this._onConnect);
this.element.addEventListener('collection:pre-add', this._onPreAdd);
this.element.addEventListener('collection:add', this._onAdd);
this.element.addEventListener('collection:pre-delete', this._onPreDelete);
this.element.addEventListener('collection:delete', this._onDelete);
}

disconnect() {
// You should always remove listeners when the controller is disconnected to avoid side effects
this.element.removeEventListener('collection:pre-connect', this._onPreConnect);
this.element.removeEventListener('collection:connect', this._onConnect);
}

_onPreConnect(event) {
// The collection is not yet connected
console.log(event.detail.allowAdd); // Access to the allow_add option of the form
console.log(event.detail.allowDelete); // Access to the allow_delete option of the form
}

_onConnect(event) {
// Same as collection:pre-connect event
}

_onPreAdd(event) {
console.log(event.detail.index); // Access to the curent index will be added
console.log(event.detail.element); // Access to the element will be added
}

_onAdd(event) {
// Same as collection:pre-add event
}

_onPreDelete(event) {
console.log(event.detail.index); // Access to the index will be removed
console.log(event.detail.element); // Access to the elemnt will be removed
}

_onDelete(event) {
// Same as collection:pre-delete event
}
}

Then in your render call, add your controller as an HTML attribute:

.. code-block:: php

$builder
// ...
->add('comments', UXCollectionType::class, [
// ...
'attr' => [
// Change the controller name
'data-controller' => 'mycollection'
]
]);