Skip to content

Commit 9d25cc9

Browse files
committed
bug #922 [Live] Simplify new form docs (weaverryan)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Live] Simplify new form docs | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Tickets | None | License | MIT I changed this section in the docs in 2.8, once it WAS legal to have non-persisted entities on a `LiveProp`. But I'm not sure why I did it this way. It's easier to default to `null`. The only caveat is that you should use `$post = $this->getFormInstance()->getData();` to get the `Post` object instead of `$this->post`, but we already show that in the docs ( did fix one spot that didn't). Commits ------- e94c03a [Live] Simplify new form docs
2 parents 320ea74 + e94c03a commit 9d25cc9

File tree

3 files changed

+22
-31
lines changed

3 files changed

+22
-31
lines changed

src/LiveComponent/doc/index.rst

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,7 @@ make it easy to deal with forms::
12871287
* with that data. The value - data - could be anything.
12881288
*/
12891289
#[LiveProp(fieldName: 'data')]
1290-
public Post $post;
1290+
public Post $post = null;
12911291

12921292
/**
12931293
* Used to re-create the PostType form for re-rendering.
@@ -1353,31 +1353,21 @@ This is possible thanks to the team work of two pieces:
13531353
yet by the user, its validation errors are cleared so that they
13541354
aren't displayed.
13551355

1356-
Making the Post Object Optional for a "New Form" Component
1357-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1358-
1359-
The previous component could be used to edit an existing post or create
1360-
a new post. But either way, the component *requires* you to pass it
1361-
a ``post`` property.
1356+
Build the "New Post" Form Component
1357+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13621358

1363-
Tou can make that optional by adding a ``mount()`` method::
1359+
The previous component can already be used to edit an existing post or create
1360+
a new post. For a new post, either pass in a new ``Post`` object to ``pass``,
1361+
or omit it entirely to let the ``post`` property default to ``null``:
13641362

1365-
#[AsLiveComponent]
1366-
class PostForm extends AbstractController
1367-
{
1368-
// ...
1369-
#[LiveProp(fieldName: 'data')]
1370-
public Post $post;
1363+
.. code-block:: twig
13711364
1372-
public function mount(Post $post = null)
1373-
{
1374-
$this->post = $post ?? new Post();
1375-
}
1376-
}
1365+
{# templates/post/new.html.twig #}
1366+
{# ... #}
13771367
1378-
If a ``post`` variable is passed to ``component()``, then it will
1379-
be passed to the ``mount()`` method where you either use it, or
1380-
create a new ``Post``.
1368+
{{ component('PostForm', {
1369+
form: form
1370+
}) }}
13811371
13821372
Form Rendering Problems
13831373
~~~~~~~~~~~~~~~~~~~~~~~
@@ -1487,7 +1477,7 @@ automatically. Let's add the ``save()`` action to the component::
14871477
$this->addFlash('success', 'Post saved!');
14881478

14891479
return $this->redirectToRoute('app_post_show', [
1490-
'id' => $this->post->getId(),
1480+
'id' => $post->getId(),
14911481
]);
14921482
}
14931483
}

src/Turbo/src/Doctrine/BroadcastListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function onFlush(EventArgs $eventArgs): void
6565
return;
6666
}
6767

68-
$em = $eventArgs->getObjectManager();
68+
$em = method_exists($eventArgs, 'getObjectManager') ? $eventArgs->getObjectManager() : $eventArgs->getEntityManager();
6969
$uow = $em->getUnitOfWork();
7070
foreach ($uow->getScheduledEntityInsertions() as $entity) {
7171
$this->storeEntitiesToPublish($em, $entity, 'createdEntities');
@@ -89,7 +89,7 @@ public function postFlush(EventArgs $eventArgs): void
8989
return;
9090
}
9191

92-
$em = $eventArgs->getObjectManager();
92+
$em = method_exists($eventArgs, 'getObjectManager') ? $eventArgs->getObjectManager() : $eventArgs->getEntityManager();
9393

9494
try {
9595
foreach ($this->createdEntities as $entity) {

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
{% if isSuccessful %}
55
<div>Welcome {{ newUserEmail}}!</div>
66
{% else %}
7-
<form
8-
novalidate
9-
data-action="live#action"
10-
data-action-name="prevent|saveRegistration"
11-
data-model="on(change)|*"
12-
>
7+
{{ form_start(form, {
8+
attr: {
9+
'novalidate': true,
10+
'data-action': 'live#action',
11+
'data-action-name': 'prevent|saveRegistration',
12+
}
13+
}) }}
1314
{{ form_row(form.email) }}
1415
{{ form_row(form.password) }}
1516
{{ form_row(form.terms) }}

0 commit comments

Comments
 (0)