Skip to content

Commit 20a6d4f

Browse files
committed
minor #417 [Website] use LiveCollectionTrait for live form collection demo (kbond)
This PR was merged into the 2.x branch. Discussion ---------- [Website] use `LiveCollectionTrait` for live form collection demo | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Tickets | n/a | License | MIT Commits ------- 16b1f4b [Website] use `LiveCollectionTrait` for live form collection demo
2 parents b34ebf9 + 16b1f4b commit 20a6d4f

File tree

3 files changed

+7
-21
lines changed

3 files changed

+7
-21
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@
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, { label: 'X', attr: { class: 'btn btn-outline-danger' } }) }}
4343
</td>
4444
</tr>
4545
{% endfor %}
4646
</tbody>
4747
</table>
4848

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>
49+
{{ form_widget(form.todoItems.vars.button_add, { label: '+ Add Item', attr: { class: 'btn btn-outline-primary' } }) }}
50+
5151
<button type="submit" class="btn btn-success" formnovalidate>Save</button>
5252
{{ form_end(form) }}
5353
</div>

0 commit comments

Comments
 (0)