Skip to content

Commit 30d4f9e

Browse files
kbond1ed
authored andcommitted
[Website] use LiveCollectionTrait for live form collection demo
1 parent 5893aca commit 30d4f9e

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
lines changed

ux.symfony.com/src/Form/TodoListForm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use App\Entity\TodoList;
66
use Symfony\Component\Form\AbstractType;
7-
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
87
use Symfony\Component\Form\FormBuilderInterface;
98
use Symfony\Component\OptionsResolver\OptionsResolver;
9+
use Symfony\UX\LiveComponent\Form\Type\LiveCollectionType;
1010

1111
class TodoListForm extends AbstractType
1212
{
@@ -16,7 +16,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
1616
->add('name', null, [
1717
'label' => 'List name',
1818
])
19-
->add('todoItems', CollectionType::class, [
19+
->add('todoItems', LiveCollectionType::class, [
2020
'entry_type' => TodoItemForm::class,
2121
'entry_options' => ['label' => false],
2222
'label' => false,

ux.symfony.com/src/Twig/TodoListFormComponent.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@
77
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
88
use Symfony\Component\Form\FormInterface;
99
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
10-
use Symfony\UX\LiveComponent\Attribute\LiveAction;
11-
use Symfony\UX\LiveComponent\Attribute\LiveArg;
1210
use Symfony\UX\LiveComponent\Attribute\LiveProp;
13-
use Symfony\UX\LiveComponent\ComponentWithFormTrait;
1411
use Symfony\UX\LiveComponent\DefaultActionTrait;
12+
use Symfony\UX\LiveComponent\LiveCollectionTrait;
1513

1614
#[AsLiveComponent('todo_list_form')]
1715
class TodoListFormComponent extends AbstractController
1816
{
19-
use ComponentWithFormTrait;
2017
use DefaultActionTrait;
18+
use LiveCollectionTrait;
2119

2220
#[LiveProp(fieldName: 'formData')]
2321
public ?TodoList $todoList;
@@ -29,16 +27,4 @@ protected function instantiateForm(): FormInterface
2927
$this->todoList
3028
);
3129
}
32-
33-
#[LiveAction]
34-
public function addItem(): void
35-
{
36-
$this->formValues['todoItems'][] = [];
37-
}
38-
39-
#[LiveAction]
40-
public function removeItem(#[LiveArg] int $index): void
41-
{
42-
unset($this->formValues['todoItems'][$index]);
43-
}
4430
}

ux.symfony.com/templates/components/todo_list_form.html.twig

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,22 @@
3939
}) }}
4040
</td>
4141
<td>
42-
<button data-action="live#action" data-action-name="removeItem(index={{ key }})" type="button" class="btn btn-outline-danger">X</button>
42+
{{ form_row(itemForm.vars.button_delete_prototype, { label: 'X', attr: itemForm.vars.button_delete_prototype.vars.attr|merge({
43+
'data-action': 'live#action',
44+
'data-action-name': 'removeCollectionItem(name=' ~ form.vars.full_name ~ ', index=' ~ itemForm.vars.name ~ ')',
45+
class: 'btn btn-outline-danger'
46+
}) }) }}
4347
</td>
4448
</tr>
4549
{% endfor %}
4650
</tbody>
4751
</table>
4852

49-
{% do form.todoItems.setRendered %}
50-
<button data-action="live#action" data-action-name="addItem" type="button" class="btn btn-outline-primary">+ Add Item</button>
53+
{{ form_widget(form.todoItems.vars.button_add_prototype, { label: '+ Add Item', attr: form.todoItems.vars.button_add_prototype.vars.attr|merge({
54+
'data-action': 'live#action',
55+
'data-action-name': 'addCollectionItem(name=' ~ form.vars.full_name ~ ')',
56+
class: 'btn btn-outline-primary'
57+
}) }) }}
58+
5159
<button type="submit" class="btn btn-success" formnovalidate>Save</button>
52-
{{ form_end(form) }}
5360
</div>

0 commit comments

Comments
 (0)