Skip to content

[Live] Simplify new form docs #922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 13 additions & 23 deletions src/LiveComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,7 @@ make it easy to deal with forms::
* with that data. The value - data - could be anything.
*/
#[LiveProp(fieldName: 'data')]
public Post $post;
public Post $post = null;

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

Making the Post Object Optional for a "New Form" Component
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The previous component could be used to edit an existing post or create
a new post. But either way, the component *requires* you to pass it
a ``post`` property.
Build the "New Post" Form Component
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

#[AsLiveComponent]
class PostForm extends AbstractController
{
// ...
#[LiveProp(fieldName: 'data')]
public Post $post;
.. code-block:: twig

public function mount(Post $post = null)
{
$this->post = $post ?? new Post();
}
}
{# templates/post/new.html.twig #}
{# ... #}

If a ``post`` variable is passed to ``component()``, then it will
be passed to the ``mount()`` method where you either use it, or
create a new ``Post``.
{{ component('PostForm', {
form: form
}) }}

Form Rendering Problems
~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -1487,7 +1477,7 @@ automatically. Let's add the ``save()`` action to the component::
$this->addFlash('success', 'Post saved!');

return $this->redirectToRoute('app_post_show', [
'id' => $this->post->getId(),
'id' => $post->getId(),
]);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Turbo/src/Doctrine/BroadcastListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function onFlush(EventArgs $eventArgs): void
return;
}

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

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

try {
foreach ($this->createdEntities as $entity) {
Expand Down
13 changes: 7 additions & 6 deletions ux.symfony.com/templates/components/RegistrationForm.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
{% if isSuccessful %}
<div>Welcome {{ newUserEmail}}!</div>
{% else %}
<form
novalidate
data-action="live#action"
data-action-name="prevent|saveRegistration"
data-model="on(change)|*"
>
{{ form_start(form, {
attr: {
'novalidate': true,
'data-action': 'live#action',
'data-action-name': 'prevent|saveRegistration',
}
}) }}
{{ form_row(form.email) }}
{{ form_row(form.password) }}
{{ form_row(form.terms) }}
Expand Down