@@ -1287,7 +1287,7 @@ make it easy to deal with forms::
1287
1287
* with that data. The value - data - could be anything.
1288
1288
*/
1289
1289
#[LiveProp(fieldName: 'data')]
1290
- public Post $post;
1290
+ public Post $post = null ;
1291
1291
1292
1292
/**
1293
1293
* 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:
1353
1353
yet by the user, its validation errors are cleared so that they
1354
1354
aren't displayed.
1355
1355
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
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1362
1358
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 ``:
1364
1362
1365
- #[AsLiveComponent]
1366
- class PostForm extends AbstractController
1367
- {
1368
- // ...
1369
- #[LiveProp(fieldName: 'data')]
1370
- public Post $post;
1363
+ .. code-block :: twig
1371
1364
1372
- public function mount(Post $post = null)
1373
- {
1374
- $this->post = $post ?? new Post();
1375
- }
1376
- }
1365
+ {# templates/post/new.html.twig #}
1366
+ {# ... #}
1377
1367
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
+ }) }}
1381
1371
1382
1372
Form Rendering Problems
1383
1373
~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments