@@ -2081,6 +2081,8 @@ then render it manually after:
2081
2081
2082
2082
{{ form_widget(form.todoItems.vars.button_add, { label: '+ Add Item', attr: { class: 'btn btn-outline-primary' } }) }}
2083
2083
2084
+ .. _validation :
2085
+
2084
2086
Validation (without a Form)
2085
2087
---------------------------
2086
2088
@@ -2302,14 +2304,14 @@ You can also trigger a specific "action" instead of a normal re-render:
2302
2304
#}
2303
2305
>
2304
2306
2305
- Binding LiveProp to URL query parameters
2307
+ Changing the URL when a LiveProp changes
2306
2308
----------------------------------------
2307
2309
2308
- .. versionadded :: 2.13
2310
+ .. versionadded :: 2.14
2309
2311
2310
- The ``url `` option was introduced in Live Components 2.13 .
2312
+ The ``url `` option was introduced in Live Components 2.14 .
2311
2313
2312
- You can bind your component LiveProp's values to URL query parameters thanks to the ``url `` option::
2314
+ If you want the URL to update when a `` LiveProp `` changes, you can do that with the ``url `` option::
2313
2315
2314
2316
// src/Components/SearchModule.php
2315
2317
namespace App\Components;
@@ -2327,43 +2329,41 @@ You can bind your component LiveProp's values to URL query parameters thanks to
2327
2329
public string $query = '';
2328
2330
}
2329
2331
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
+ Now, when the user changes the value of the ``query `` prop, a query parameter in the URL will be updated to reflect the
2333
+ new state of your component, for example: ``https://my.domain/search?query=my+search+string ``.
2332
2334
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
+ If you load this URL in your browser, the `` LiveProp `` value will be initialized using the query string
2336
+ (e.g. `` my search string``) .
2335
2337
2336
2338
.. note ::
2337
2339
2338
- URL is updated in place, no new entry is added to the browser's history .
2340
+ The URL is changed via `` history.replaceState() ``. So no new entry is added.
2339
2341
2340
2342
.. warning ::
2341
2343
2342
2344
You can use multiple components with URL bindings in the same page, as long as bound field names don't collide.
2343
2345
Otherwise, you will observe unexpected behaviors.
2344
2346
2345
- Supported data types
2347
+ Supported Data Types
2346
2348
~~~~~~~~~~~~~~~~~~~~
2347
2349
2348
2350
You can use scalars, arrays and objects in your URL bindings:
2349
2351
2350
2352
============================================ =================================================
2351
- ``prop `` value URL representation
2353
+ JavaScript ``prop `` value URL representation
2352
2354
============================================ =================================================
2353
2355
``'some search string' `` ``prop=some+search+string ``
2354
2356
``42 `` ``prop=42 ``
2355
2357
``['foo', 'bar'] `` ``prop[0]=foo&prop[1]=bar ``
2356
2358
``{ foo: 'bar', baz: 42 } `` ``prop[foo]=bar&prop[baz]=42 ``
2357
2359
2358
2360
2359
- .. note::
2360
-
2361
- Type consistency is enforced by the LiveComponent internal hydrator when the component is initialized with query
2362
- parameters. If a value could not be hydrated properly, it will be ignored to avoid unfriendly errors for the end
2363
- user.
2361
+ When a page is loaded with a query parameter that's bound to a ``LiveProp`` (e.g. ``/search?query=my+search+string``),
2362
+ the value - ``my search string `` - goes through the hydration system before it's set onto the property. If a value can't
2363
+ be hydrated, it will be ignored.
2364
2364
2365
- Multiple bindings
2366
- ~~~~~~~~~~~~~~~~~
2365
+ Multiple Query Parameter Bindings
2366
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2367
2367
2368
2368
You can use as many URL bindings as you want in your component. To ensure the state is fully represented in the URL,
2369
2369
all bound props will be set as query parameters, even if their values didn't change.
@@ -2384,17 +2384,15 @@ For example, if you declare the following bindings::
2384
2384
}
2385
2385
2386
2386
2387
- And you only set the ``query `` value, then your URL will be updated to ``https://my.domain/search?query=my+query+string&mode=fulltext ``.
2387
+ And you only set the ``query `` value, then your URL will be updated to
2388
+ ``https://my.domain/search?query=my+query+string&mode=fulltext ``.
2388
2389
2389
- Validation
2390
- ~~~~~~~~~~
2391
-
2392
- .. caution ::
2393
-
2394
- It is recommended to validate data provided through query string parameters, so you can prevent malicious inputs
2395
- from being processed and rendered.
2390
+ Validating the Query Parameter Values
2391
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2396
2392
2397
- To validate your component state when it is rendered for the first time, you have to setup a `PostMount hook `_::
2393
+ Like any writable ``LiveProp ``, because the user can modify this value, you should consider adding
2394
+ :ref: `validation <validation >`. When you bind a ``LiveProp `` to the URL, the initial value is not automatically
2395
+ validated. To validate it, you have to set up a `PostMount hook `_::
2398
2396
2399
2397
// ...
2400
2398
use Symfony\Component\Validator\Constraints as Assert;
@@ -2416,8 +2414,11 @@ To validate your component state when it is rendered for the first time, you hav
2416
2414
#[PostMount]
2417
2415
public function postMount(): void
2418
2416
{
2419
- // Validate 'mode' field without throwing an exception
2420
- $this->validateField('mode', false);
2417
+ // Validate 'mode' field without throwing an exception, so the component can be mounted anyway and a
2418
+ // validation error can be shown to the user
2419
+ if (!$this->validateField('mode', false)) {
2420
+ // Do something when validation fails
2421
+ }
2421
2422
}
2422
2423
2423
2424
// ...
0 commit comments