Skip to content

Commit f88979d

Browse files
committed
[Live] Simplify new form docs
1 parent 320ea74 commit f88979d

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

src/LiveComponent/doc/index.rst

Lines changed: 12 additions & 22 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
~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)