Skip to content

Commit 7a740f6

Browse files
committed
Rewords
1 parent f3b89a5 commit 7a740f6

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

src/LiveComponent/doc/index.rst

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,6 +2081,8 @@ then render it manually after:
20812081

20822082
{{ form_widget(form.todoItems.vars.button_add, { label: '+ Add Item', attr: { class: 'btn btn-outline-primary' } }) }}
20832083

2084+
.. _validation:
2085+
20842086
Validation (without a Form)
20852087
---------------------------
20862088

@@ -2302,14 +2304,14 @@ You can also trigger a specific "action" instead of a normal re-render:
23022304
#}
23032305
>
23042306

2305-
Binding LiveProp to URL query parameters
2307+
Changing the URL when a LiveProp changes
23062308
----------------------------------------
23072309

2308-
.. versionadded:: 2.13
2310+
.. versionadded:: 2.14
23092311

2310-
The ``url`` option was introduced in Live Components 2.13.
2312+
The ``url`` option was introduced in Live Components 2.14.
23112313

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::
23132315

23142316
// src/Components/SearchModule.php
23152317
namespace App\Components;
@@ -2327,43 +2329,41 @@ You can bind your component LiveProp's values to URL query parameters thanks to
23272329
public string $query = '';
23282330
}
23292331

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``.
23322334

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``).
23352337

23362338
.. note::
23372339

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.
23392341

23402342
.. warning::
23412343

23422344
You can use multiple components with URL bindings in the same page, as long as bound field names don't collide.
23432345
Otherwise, you will observe unexpected behaviors.
23442346

2345-
Supported data types
2347+
Supported Data Types
23462348
~~~~~~~~~~~~~~~~~~~~
23472349

23482350
You can use scalars, arrays and objects in your URL bindings:
23492351

23502352
============================================ =================================================
2351-
``prop`` value URL representation
2353+
JavaScript ``prop`` value URL representation
23522354
============================================ =================================================
23532355
``'some search string'`` ``prop=some+search+string``
23542356
``42`` ``prop=42``
23552357
``['foo', 'bar']`` ``prop[0]=foo&prop[1]=bar``
23562358
``{ foo: 'bar', baz: 42 }`` ``prop[foo]=bar&prop[baz]=42``
23572359

23582360

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.
23642364

2365-
Multiple bindings
2366-
~~~~~~~~~~~~~~~~~
2365+
Multiple Query Parameter Bindings
2366+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23672367

23682368
You can use as many URL bindings as you want in your component. To ensure the state is fully represented in the URL,
23692369
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::
23842384
}
23852385

23862386

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``.
23882389

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+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23962392

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`_::
23982396

23992397
// ...
24002398
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
24162414
#[PostMount]
24172415
public function postMount(): void
24182416
{
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+
}
24212422
}
24222423

24232424
// ...

0 commit comments

Comments
 (0)