@@ -2438,11 +2438,6 @@ If you load this URL in your browser, the ``LiveProp`` value will be initialized
24382438
24392439 The URL is changed via ``history.replaceState() ``. So no new entry is added.
24402440
2441- .. warning ::
2442-
2443- You can use multiple components with URL bindings in the same page, as long as bound field names don't collide.
2444- Otherwise, you will observe unexpected behaviors.
2445-
24462441Supported Data Types
24472442~~~~~~~~~~~~~~~~~~~~
24482443
@@ -2486,6 +2481,65 @@ For example, if you declare the following bindings::
24862481And you only set the ``query `` value, then your URL will be updated to
24872482``https://my.domain/search?query=my+query+string&mode=fulltext ``.
24882483
2484+ Controlling the Query Parameter Name
2485+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2486+
2487+ .. versionadded :: 2.17
2488+
2489+ The ``as `` option was added in LiveComponents 2.17.
2490+
2491+
2492+ Instead of using the prop's field name as the query parameter name, you can use the ``as `` option in your ``LiveProp ``
2493+ definition::
2494+
2495+ // ...
2496+ use Symfony\UX\LiveComponent\Metadata\UrlMapping;
2497+
2498+ #[AsLiveComponent]
2499+ class SearchModule
2500+ {
2501+ #[LiveProp(writable: true, url: new UrlMapping(as: 'q')]
2502+ public string $query = '';
2503+
2504+ // ...
2505+ }
2506+
2507+ Then the ``query `` value will appear in the URL like ``https://my.domain/search?q=my+query+string ``.
2508+
2509+ If you need to change the parameter name on a specific page, you can leverage the :ref: `modifier <modifier >` option::
2510+
2511+ // ...
2512+ use Symfony\UX\LiveComponent\Metadata\UrlMapping;
2513+
2514+ #[AsLiveComponent]
2515+ class SearchModule
2516+ {
2517+ #[LiveProp(writable: true, url: true, modifier: 'modifyQueryProp')]
2518+ public string $query = '';
2519+
2520+ #[LiveProp]
2521+ public ?string $alias = null;
2522+
2523+ public function modifyQueryProp(LiveProp $liveProp): LiveProp
2524+ {
2525+ if ($this->alias) {
2526+ $liveProp = $liveProp->withUrl(new UrlMapping(as: $this->alias));
2527+ }
2528+ return $liveProp;
2529+ }
2530+ }
2531+
2532+ .. code-block :: twig
2533+
2534+ <twig:SearchModule alias="q" />
2535+
2536+ This way you can also use the component multiple times in the same page and avoid collisions in parameter names:
2537+
2538+ .. code-block :: twig
2539+
2540+ <twig:SearchModule alias="q1" />
2541+ <twig:SearchModule alias="q2" />
2542+
24892543 Validating the Query Parameter Values
24902544~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24912545
@@ -2513,8 +2567,8 @@ validated. To validate it, you have to set up a `PostMount hook`_::
25132567 #[PostMount]
25142568 public function postMount(): void
25152569 {
2516- // Validate 'mode' field without throwing an exception, so the component can be mounted anyway and a
2517- // validation error can be shown to the user
2570+ // Validate 'mode' field without throwing an exception, so the component can
2571+ // be mounted anyway and a validation error can be shown to the user
25182572 if (!$this->validateField('mode', false)) {
25192573 // Do something when validation fails
25202574 }
@@ -3450,6 +3504,8 @@ the change of one specific key::
34503504 }
34513505 }
34523506
3507+ .. _modifier :
3508+
34533509Set LiveProp Options Dynamically
34543510~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34553511
0 commit comments