Skip to content

Commit fd208f8

Browse files
yalitsmnandre
authored andcommitted
[LiveComponent] Add Documentation for testing with LiveCollectionType
1 parent 04f40fd commit fd208f8

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/LiveComponent/doc/index.rst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3689,6 +3689,9 @@ Test Helper
36893689

36903690
The test helper was added in LiveComponents 2.11.
36913691

3692+
Interact With Live-Components
3693+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3694+
36923695
For testing, you can use the ``InteractsWithLiveComponents`` trait which
36933696
uses Symfony's test client to render and make requests to your components::
36943697

@@ -3775,6 +3778,54 @@ uses Symfony's test client to render and make requests to your components::
37753778
The ``InteractsWithLiveComponents`` trait can only be used in tests that extend
37763779
``Symfony\Bundle\FrameworkBundle\Test\KernelTestCase``.
37773780

3781+
Test LiveCollectionType
3782+
~~~~~~~~~~~~~~~~~~~~~~~
3783+
3784+
To test the submission of a form within a Live Component (with the above ``submitForm`` helper) containing a ``LiveCollectionType``, you first need to programmatically add the desired number of entries to the form, replicating the action of clicking the "Add" button.
3785+
3786+
So, if the following are the forms used::
3787+
3788+
use Symfony\UX\LiveComponent\Form\Type\LiveCollectionType;
3789+
3790+
// Parent FormType used in the Live Component
3791+
class LiveCollectionFormType extends AbstractType
3792+
{
3793+
public function buildForm(FormBuilderInterface $builder, array $options): void
3794+
{
3795+
$builder->add('children', LiveCollectionType::class, [
3796+
'entry_type' => ChildFormType::class,
3797+
])
3798+
;
3799+
}
3800+
}
3801+
3802+
// Child Form Type used for each entry in the collection
3803+
class ChildFormType extends AbstractType
3804+
{
3805+
public function buildForm(FormBuilderInterface $builder, array $options): void
3806+
{
3807+
$builder
3808+
->add('name', TextType::class)
3809+
->add('age', IntegerType::class)
3810+
;
3811+
}
3812+
}
3813+
3814+
Use the addCollectionItem method from the LiveCollectionTrait to dynamically add entries to the children field of the form before submitting it::
3815+
3816+
// Call the addCollectionItem method as many times as needed, specifying the name of the collection field.
3817+
$component->call('addCollectionItem', ['name' => 'children']);
3818+
$component->call('addCollectionItem', ['name' => 'children']);
3819+
//... can be called as many times as you need entries in your 'children' field
3820+
3821+
// ... then submit the form by providing data for all the fields in the ChildFormType for each added entry:
3822+
$component->submitForm([ 'live_collection_form' => [
3823+
'children' => [
3824+
['name' => 'childName1', 'age' => 10],
3825+
['name' => 'childName2', 'age' => 15],
3826+
]
3827+
]]);
3828+
37783829
Backward Compatibility promise
37793830
------------------------------
37803831

0 commit comments

Comments
 (0)