Skip to content

Commit 8fdcd69

Browse files
committed
minor #2517 [LiveComponent] Add Documentation for testing with LiveCollectionType (yalit)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [LiveComponent] Add Documentation for testing with LiveCollectionType | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Issues | Fix #2512 | License | MIT As mentioned in the #2512, I had trouble testing the submission of a form containing a LiveCollectionType field due to lack of proper guidance in the documentation. Hence this pull request to propose an addendum in the documentation linked to that capability Commits ------- fd208f8 [LiveComponent] Add Documentation for testing with LiveCollectionType
2 parents df6d50b + fd208f8 commit 8fdcd69

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/LiveComponent/doc/index.rst

+51
Original file line numberDiff line numberDiff line change
@@ -3722,6 +3722,9 @@ Test Helper
37223722

37233723
The test helper was added in LiveComponents 2.11.
37243724

3725+
Interact With Live-Components
3726+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3727+
37253728
For testing, you can use the ``InteractsWithLiveComponents`` trait which
37263729
uses Symfony's test client to render and make requests to your components::
37273730

@@ -3808,6 +3811,54 @@ uses Symfony's test client to render and make requests to your components::
38083811
The ``InteractsWithLiveComponents`` trait can only be used in tests that extend
38093812
``Symfony\Bundle\FrameworkBundle\Test\KernelTestCase``.
38103813

3814+
Test LiveCollectionType
3815+
~~~~~~~~~~~~~~~~~~~~~~~
3816+
3817+
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.
3818+
3819+
So, if the following are the forms used::
3820+
3821+
use Symfony\UX\LiveComponent\Form\Type\LiveCollectionType;
3822+
3823+
// Parent FormType used in the Live Component
3824+
class LiveCollectionFormType extends AbstractType
3825+
{
3826+
public function buildForm(FormBuilderInterface $builder, array $options): void
3827+
{
3828+
$builder->add('children', LiveCollectionType::class, [
3829+
'entry_type' => ChildFormType::class,
3830+
])
3831+
;
3832+
}
3833+
}
3834+
3835+
// Child Form Type used for each entry in the collection
3836+
class ChildFormType extends AbstractType
3837+
{
3838+
public function buildForm(FormBuilderInterface $builder, array $options): void
3839+
{
3840+
$builder
3841+
->add('name', TextType::class)
3842+
->add('age', IntegerType::class)
3843+
;
3844+
}
3845+
}
3846+
3847+
Use the addCollectionItem method from the LiveCollectionTrait to dynamically add entries to the children field of the form before submitting it::
3848+
3849+
// Call the addCollectionItem method as many times as needed, specifying the name of the collection field.
3850+
$component->call('addCollectionItem', ['name' => 'children']);
3851+
$component->call('addCollectionItem', ['name' => 'children']);
3852+
//... can be called as many times as you need entries in your 'children' field
3853+
3854+
// ... then submit the form by providing data for all the fields in the ChildFormType for each added entry:
3855+
$component->submitForm([ 'live_collection_form' => [
3856+
'children' => [
3857+
['name' => 'childName1', 'age' => 10],
3858+
['name' => 'childName2', 'age' => 15],
3859+
]
3860+
]]);
3861+
38113862
Backward Compatibility promise
38123863
------------------------------
38133864

0 commit comments

Comments
 (0)