@@ -2489,11 +2489,6 @@ If you load this URL in your browser, the ``LiveProp`` value will be initialized
24892489
24902490 The URL is changed via ``history.replaceState() ``. So no new entry is added.
24912491
2492- .. warning ::
2493-
2494- You can use multiple components with URL bindings in the same page, as long as bound field names don't collide.
2495- Otherwise, you will observe unexpected behaviors.
2496-
24972492Supported Data Types
24982493~~~~~~~~~~~~~~~~~~~~
24992494
@@ -2537,6 +2532,65 @@ For example, if you declare the following bindings::
25372532And you only set the ``query `` value, then your URL will be updated to
25382533``https://my.domain/search?query=my+query+string&mode=fulltext ``.
25392534
2535+ Controlling the Query Parameter Name
2536+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2537+
2538+ .. versionadded :: 2.17
2539+
2540+ The ``as `` option was added in LiveComponents 2.17.
2541+
2542+
2543+ Instead of using the prop's field name as the query parameter name, you can use the ``as `` option in your ``LiveProp ``
2544+ definition::
2545+
2546+ // ...
2547+ use Symfony\UX\LiveComponent\Metadata\UrlMapping;
2548+
2549+ #[AsLiveComponent]
2550+ class SearchModule
2551+ {
2552+ #[LiveProp(writable: true, url: new UrlMapping(as: 'q')]
2553+ public string $query = '';
2554+
2555+ // ...
2556+ }
2557+
2558+ Then the ``query `` value will appear in the URL like ``https://my.domain/search?q=my+query+string ``.
2559+
2560+ If you need to change the parameter name on a specific page, you can leverage the :ref: `modifier <modifier >` option::
2561+
2562+ // ...
2563+ use Symfony\UX\LiveComponent\Metadata\UrlMapping;
2564+
2565+ #[AsLiveComponent]
2566+ class SearchModule
2567+ {
2568+ #[LiveProp(writable: true, url: true, modifier: 'modifyQueryProp')]
2569+ public string $query = '';
2570+
2571+ #[LiveProp]
2572+ public ?string $alias = null;
2573+
2574+ public function modifyQueryProp(LiveProp $liveProp): LiveProp
2575+ {
2576+ if ($this->alias) {
2577+ $liveProp = $liveProp->withUrl(new UrlMapping(as: $this->alias));
2578+ }
2579+ return $liveProp;
2580+ }
2581+ }
2582+
2583+ .. code-block :: html+twig
2584+
2585+ <twig:SearchModule alias="q" />
2586+
2587+ This way you can also use the component multiple times in the same page and avoid collisions in parameter names:
2588+
2589+ .. code-block :: html+twig
2590+
2591+ <twig:SearchModule alias="q1" />
2592+ <twig:SearchModule alias="q2" />
2593+
25402594Validating the Query Parameter Values
25412595~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25422596
@@ -2564,8 +2618,8 @@ validated. To validate it, you have to set up a `PostMount hook`_::
25642618 #[PostMount]
25652619 public function postMount(): void
25662620 {
2567- // Validate 'mode' field without throwing an exception, so the component can be mounted anyway and a
2568- // validation error can be shown to the user
2621+ // Validate 'mode' field without throwing an exception, so the component can
2622+ // be mounted anyway and a validation error can be shown to the user
25692623 if (!$this->validateField('mode', false)) {
25702624 // Do something when validation fails
25712625 }
@@ -3501,6 +3555,8 @@ the change of one specific key::
35013555 }
35023556 }
35033557
3558+ .. _modifier :
3559+
35043560Set LiveProp Options Dynamically
35053561~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35063562
0 commit comments