Skip to content

Commit 92ca639

Browse files
committed
[Live] make form available directly with ComponentWithFormTrait
1 parent bf4f67b commit 92ca639

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

src/LiveComponent/composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
"conflict": {
4848
"symfony/config": "<5.4.0"
4949
},
50+
"repositories": [
51+
{
52+
"type": "path",
53+
"url": "../TwigComponent"
54+
}
55+
],
5056
"config": {
5157
"sort-packages": true
5258
},

src/LiveComponent/src/ComponentWithFormTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\UX\LiveComponent\Attribute\BeforeReRender;
1919
use Symfony\UX\LiveComponent\Attribute\LiveProp;
2020
use Symfony\UX\LiveComponent\Util\LiveFormUtility;
21+
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;
2122
use Symfony\UX\TwigComponent\Attribute\PostMount;
2223

2324
/**
@@ -27,6 +28,7 @@
2728
*/
2829
trait ComponentWithFormTrait
2930
{
31+
#[ExposeInTemplate(name: 'form', getter: 'getForm')]
3032
private ?FormView $formView = null;
3133
private ?FormInterface $formInstance = null;
3234

src/LiveComponent/src/Resources/doc/index.rst

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,12 @@ recreate the *same* form, we pass in the ``Post`` object and set it as a
813813
``LiveProp``.
814814

815815
The template for this component will render the form, which is available
816-
as ``this.form`` thanks to the trait:
816+
as ``form`` thanks to the trait:
817+
818+
.. versionadded:: 2.1
819+
820+
The ability to access ``form`` directly in your component's template
821+
was added in LiveComponents 2.1. Previously ``this.form`` was required.
817822

818823
.. code-block:: twig
819824
@@ -833,13 +838,13 @@ as ``this.form`` thanks to the trait:
833838
#}
834839
data-action="change->live#update"
835840
>
836-
{{ form_start(this.form) }}
837-
{{ form_row(this.form.title) }}
838-
{{ form_row(this.form.slug) }}
839-
{{ form_row(this.form.content) }}
841+
{{ form_start(form) }}
842+
{{ form_row(form.title) }}
843+
{{ form_row(form.slug) }}
844+
{{ form_row(form.content) }}
840845
841846
<button>Save</button>
842-
{{ form_end(this.form) }}
847+
{{ form_end(form) }}
843848
</div>
844849
845850
Mostly, this is a pretty boring template! It includes the normal
@@ -1027,7 +1032,7 @@ Finally, tell the ``form`` element to use this action:
10271032
{# templates/components/post_form.html.twig #}
10281033
{# ... #}
10291034
1030-
{{ form_start(this.form, {
1035+
{{ form_start(form, {
10311036
attr: {
10321037
'data-action': 'live#action',
10331038
'data-action-name': 'prevent|save'
@@ -1148,11 +1153,11 @@ and ``removeComment()`` actions:
11481153
.. code-block:: twig
11491154
11501155
<div{{ attributes }}>
1151-
{{ form_start(this.form) }}
1152-
{{ form_row(this.form.title) }}
1156+
{{ form_start(form) }}
1157+
{{ form_row(form.title) }}
11531158
11541159
<h3>Comments:</h3>
1155-
{% for key, commentForm in this.form.comments %}
1160+
{% for key, commentForm in form.comments %}
11561161
<button
11571162
data-action="live#action"
11581163
data-action-name="removeComment(index={{ key }})"
@@ -1164,7 +1169,7 @@ and ``removeComment()`` actions:
11641169
</div>
11651170
11661171
{# avoid an extra label for this field #}
1167-
{% do this.form.comments.setRendered %}
1172+
{% do form.comments.setRendered %}
11681173
11691174
<button
11701175
data-action="live#action"
@@ -1173,7 +1178,7 @@ and ``removeComment()`` actions:
11731178
>+ Add Comment</button>
11741179
11751180
<button type="submit" >Save</button>
1176-
{{ form_end(this.form) }}
1181+
{{ form_end(form) }}
11771182
</div>
11781183
11791184
Done! Behind the scenes, it works like this:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<div{{ attributes }}>
2-
{{ form(this.form) }}
2+
{{ form(form) }}
33
</div>

0 commit comments

Comments
 (0)