@@ -2302,6 +2302,114 @@ You can also trigger a specific "action" instead of a normal re-render:
2302
2302
#}
2303
2303
>
2304
2304
2305
+ Binding LiveProp to URL query parameters
2306
+ ----------------------------------------
2307
+
2308
+ .. versionadded :: 2.13
2309
+
2310
+ The ``url `` option was introduced in Live Components 2.13.
2311
+
2312
+ You can bind your component LiveProp's values to URL query parameters thanks to the ``url `` option::
2313
+
2314
+ // src/Components/SearchModule.php
2315
+ namespace App\Components;
2316
+
2317
+ use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
2318
+ use Symfony\UX\LiveComponent\Attribute\LiveProp;
2319
+ use Symfony\UX\LiveComponent\DefaultActionTrait;
2320
+
2321
+ #[AsLiveComponent]
2322
+ class SearchModule
2323
+ {
2324
+ use DefaultActionTrait;
2325
+
2326
+ #[LiveProp(writable: true, url: true)]
2327
+ public string $query = '';
2328
+ }
2329
+
2330
+ Now if you change the value of the ``query `` prop, the url will be updated to reflect the new state of your
2331
+ component, for example: ``https://my.domain/search?query=my+search+string ``.
2332
+
2333
+ Then, if you load this URL in your browser, your component will be initialized with the values provided in the query
2334
+ string.
2335
+
2336
+ Supported data types
2337
+ ~~~~~~~~~~~~~~~~~~~~
2338
+
2339
+ You can use scalars, arrays and objects in your URL bindings:
2340
+
2341
+ ============================================ =================================================
2342
+ ``prop `` value URL representation
2343
+ ============================================ =================================================
2344
+ ``'some search string' `` ``prop=some+search+string ``
2345
+ ``42 `` ``prop=42 ``
2346
+ ``['foo', 'bar'] `` ``prop[0]=foo&prop[1]=bar ``
2347
+ ``{ foo: 'bar', baz: 42 } `` ``prop[foo]=bar&prop[baz]=42 ``
2348
+
2349
+
2350
+ .. note::
2351
+
2352
+ Type consistency is enforced by the LiveComponent internal hydrator when the component is initialized with query
2353
+ parameters. If a value could not be hydrated properly, it will be ignored to avoid unfriendly errors for the end
2354
+ user.
2355
+
2356
+ Multiple bindings
2357
+ ~~~~~~~~~~~~~~~~~
2358
+
2359
+ You can use as many URL bindings as you want in your component. To ensure the state is fully represented in the URL,
2360
+ all bound props will be added to query parameters, even if their values didn't change.
2361
+
2362
+ For example, if you declare the following bindings::
2363
+
2364
+ // ...
2365
+ #[AsLiveComponent]
2366
+ class SearchModule
2367
+ {
2368
+ #[LiveProp(writable: true, url: true)]
2369
+ public string $query = '';
2370
+
2371
+ #[LiveProp(writable: true, url: true)]
2372
+ public string $mode = 'fulltext';
2373
+
2374
+ // ...
2375
+ }
2376
+
2377
+
2378
+ And you only set the ``query `` value, then your URL will be updated to ``https://my.domain/search?query=my+query+string&mode=fulltext ``.
2379
+
2380
+ Validation
2381
+ ~~~~~~~~~~
2382
+
2383
+ To validate your component state when it is rendered for the first time, you have to setup a `PostMount hook `_::
2384
+
2385
+ // ...
2386
+ use Symfony\Component\Validator\Constraints as Assert;
2387
+ use Symfony\UX\LiveComponent\ValidatableComponentTrait;
2388
+ use Symfony\UX\TwigComponent\Attribute\PostMount;
2389
+
2390
+ #[AsLiveComponent]
2391
+ class SearchModule
2392
+ {
2393
+ use ValidatableComponentTrait;
2394
+
2395
+ #[LiveProp(writable: true, url: true)]
2396
+ public string $query = '';
2397
+
2398
+ #[LiveProp(writable: true, url: true)]
2399
+ #[Assert\NotBlank]
2400
+ public string $mode = 'fulltext';
2401
+
2402
+ #[PostMount]
2403
+ public function postMount(): void
2404
+ {
2405
+ // Validate 'mode' field without throwing an exception
2406
+ $this->validateField('mode', false);
2407
+ }
2408
+
2409
+ // ..
2410
+ }
2411
+
2412
+
2305
2413
.. _emit :
2306
2414
2307
2415
Communication Between Components: Emitting Events
@@ -3315,3 +3423,4 @@ bound to Symfony's BC policy for the moment.
3315
3423
.. _`Symfony's built-in form theming techniques` : https://symfony.com/doc/current/form/form_themes.html
3316
3424
.. _`pass content to Twig Components` : https://symfony.com/bundles/ux-twig-component/current/index.html#passing-blocks
3317
3425
.. _`Twig Component debug command` : https://symfony.com/bundles/ux-twig-component/current/index.html#debugging-components
3426
+ .. _`PostMount hook` : https://symfony.com/bundles/ux-twig-component/current/index.html#postmount-hook
0 commit comments