Skip to content

Commit ffcd049

Browse files
committed
Merge branch '4.4' into 5.1
* 4.4: addressed issue #11786 Add array example on ChoiceType choice_attr option [#14340] Some minor textual changes [Collection forms] Make javascript generic Updating the Installer Related Instructions
2 parents 281d758 + 692b84f commit ffcd049

File tree

3 files changed

+79
-92
lines changed

3 files changed

+79
-92
lines changed

components/serializer.rst

+42-68
Original file line numberDiff line numberDiff line change
@@ -815,8 +815,20 @@ The ``XmlEncoder`` will encode this object like that::
815815
<bar>1</bar>
816816
</response>
817817

818-
Be aware that this encoder will consider keys beginning with ``@`` as attributes, and will use
819-
the key ``#comment`` for encoding XML comments::
818+
The special ``#`` key can be used to define the data of a node::
819+
820+
['foo' => ['@bar' => 'value', '#' => 'baz']];
821+
822+
// is encoded as follows:
823+
// <?xml version="1.0"?>
824+
// <response>
825+
// <foo bar="value">
826+
// baz
827+
// </foo>
828+
// </response>
829+
830+
Furthermore, keys beginning with ``@`` will be considered attributes, and
831+
the key ``#comment`` can be used for encoding XML comments::
820832

821833
$encoder = new XmlEncoder();
822834
$encoder->encode([
@@ -842,6 +854,34 @@ always as a collection.
842854
changed with the optional ``$encoderIgnoredNodeTypes`` argument of the
843855
``XmlEncoder`` class constructor.
844856

857+
The ``XmlEncoder`` Context Options
858+
..................................
859+
860+
The ``encode()`` method defines a third optional parameter called ``context``
861+
which defines the configuration options for the XmlEncoder an associative array::
862+
863+
$xmlEncoder->encode($array, 'xml', $context);
864+
865+
These are the options available:
866+
867+
``xml_format_output``
868+
If set to true, formats the generated XML with line breaks and indentation.
869+
870+
``xml_version``
871+
Sets the XML version attribute (default: ``1.1``).
872+
873+
``xml_encoding``
874+
Sets the XML encoding attribute (default: ``utf-8``).
875+
876+
``xml_standalone``
877+
Adds standalone attribute in the generated XML (default: ``true``).
878+
879+
``xml_root_node_name``
880+
Sets the root node name (default: ``response``).
881+
882+
``remove_empty_tags``
883+
If set to true, removes all empty tags in the generated XML (default: ``false``).
884+
845885
The ``YamlEncoder``
846886
~~~~~~~~~~~~~~~~~~~
847887

@@ -1153,72 +1193,6 @@ you indicate that you're expecting an array instead of a single object::
11531193
$data = ...; // The serialized data from the previous example
11541194
$persons = $serializer->deserialize($data, 'Acme\Person[]', 'json');
11551195

1156-
The ``XmlEncoder``
1157-
------------------
1158-
1159-
This encoder transforms arrays into XML and vice versa. For example, take an
1160-
object normalized as following::
1161-
1162-
['foo' => [1, 2], 'bar' => true];
1163-
1164-
The ``XmlEncoder`` encodes this object as follows:
1165-
1166-
.. code-block:: xml
1167-
1168-
<?xml version="1.0"?>
1169-
<response>
1170-
<foo>1</foo>
1171-
<foo>2</foo>
1172-
<bar>1</bar>
1173-
</response>
1174-
1175-
The array keys beginning with ``@`` are considered XML attributes::
1176-
1177-
['foo' => ['@bar' => 'value']];
1178-
1179-
// is encoded as follows:
1180-
// <?xml version="1.0"?>
1181-
// <response>
1182-
// <foo bar="value"/>
1183-
// </response>
1184-
1185-
Use the special ``#`` key to define the data of a node::
1186-
1187-
['foo' => ['@bar' => 'value', '#' => 'baz']];
1188-
1189-
// is encoded as follows:
1190-
// <?xml version="1.0"?>
1191-
// <response>
1192-
// <foo bar="value">baz</foo>
1193-
// </response>
1194-
1195-
Context
1196-
~~~~~~~
1197-
1198-
The ``encode()`` method defines a third optional parameter called ``context``
1199-
which defines the configuration options for the XmlEncoder an associative array::
1200-
1201-
$xmlEncoder->encode($array, 'xml', $context);
1202-
1203-
These are the options available:
1204-
1205-
``xml_format_output``
1206-
If set to true, formats the generated XML with line breaks and indentation.
1207-
1208-
``xml_version``
1209-
Sets the XML version attribute (default: ``1.1``).
1210-
1211-
``xml_encoding``
1212-
Sets the XML encoding attribute (default: ``utf-8``).
1213-
1214-
``xml_standalone``
1215-
Adds standalone attribute in the generated XML (default: ``true``).
1216-
1217-
``xml_root_node_name``
1218-
Sets the root node name (default: ``response``).
1219-
1220-
``remove_empty_tags``
1221-
If set to true, removes all empty tags in the generated XML (default: ``false``).
12221196

12231197
The ``CsvEncoder``
12241198
------------------

form/form_collections.rst

+23-24
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,13 @@ the following ``data-prototype`` attribute to the existing ``<ul>`` in your temp
242242

243243
.. code-block:: html+twig
244244

245-
<ul class="tags" data-prototype="{{ form_widget(form.tags.vars.prototype)|e('html_attr') }}">
245+
<ul class="tags" data-prototype="{{ form_widget(form.tags.vars.prototype)|e('html_attr') }}"></ul>
246+
247+
Now add a button just next to the ``<ul>`` to dynamically add a new tag
248+
249+
.. code-block:: html+twig
250+
251+
<button type="button" class="add_item_link" data-collection-holder-class="tags">Add a tag</button>
246252

247253
On the rendered page, the result will look something like this:
248254

@@ -274,38 +280,27 @@ On the rendered page, the result will look something like this:
274280
and you need to adjust the following JavaScript accordingly.
275281

276282
The goal of this section will be to use JavaScript to read this attribute
277-
and dynamically add new tag forms when the user clicks a "Add a tag" link.
283+
and dynamically add new tag forms when the user clicks the "Add a tag" button.
278284
This example uses jQuery and assumes you have it included somewhere on your page.
279285

280-
Add a ``script`` tag somewhere on your page so you can start writing some JavaScript.
281-
282-
First, add a link to the bottom of the "tags" list via JavaScript. Second,
283-
bind to the "click" event of that link so you can add a new tag form (``addTagForm()``
284-
will be show next):
286+
Add a ``script`` tag somewhere on your page so you can start writing some
287+
JavaScript. In this script, bind to the "click" event of the "Add a tag"
288+
button so you can add a new tag form (``addFormToCollection()`` will be show next):
285289

286290
.. code-block:: javascript
287291
288-
var $collectionHolder;
289-
290-
// setup an "add a tag" link
291-
var $addTagButton = $('<button type="button" class="add_tag_link">Add a tag</button>');
292-
var $newLinkLi = $('<li></li>').append($addTagButton);
293-
294292
jQuery(document).ready(function() {
295293
// Get the ul that holds the collection of tags
296-
$collectionHolder = $('ul.tags');
297-
298-
// add the "add a tag" anchor and li to the tags ul
299-
$collectionHolder.append($newLinkLi);
300-
294+
var $tagsCollectionHolder = $('ul.tags');
301295
// count the current form inputs we have (e.g. 2), use that as the new
302296
// index when inserting a new item (e.g. 2)
303-
$collectionHolder.data('index', $collectionHolder.find('input').length);
297+
$tagsCollectionHolder.data('index', $tagsCollectionHolder.find('input').length);
304298
305-
$addTagButton.on('click', function(e) {
299+
$('body').on('click', '.add_item_link', function(e) {
300+
var $collectionHolderClass = $(e.currentTarget).data('collectionHolderClass');
306301
// add a new tag form (see next code block)
307-
addTagForm($collectionHolder, $newLinkLi);
308-
});
302+
addFormToCollection($collectionHolderClass);
303+
})
309304
});
310305
311306
The ``addTagForm()`` function's job will be to use the ``data-prototype`` attribute
@@ -319,7 +314,10 @@ one example:
319314

320315
.. code-block:: javascript
321316
322-
function addTagForm($collectionHolder, $newLinkLi) {
317+
function addFormToCollection($collectionHolderClass) {
318+
// Get the ul that holds the collection of tags
319+
var $collectionHolder = $('.' + $collectionHolderClass);
320+
323321
// Get the data-prototype explained earlier
324322
var prototype = $collectionHolder.data('prototype');
325323
@@ -341,7 +339,8 @@ one example:
341339
342340
// Display the form in the page in an li, before the "Add a tag" link li
343341
var $newFormLi = $('<li></li>').append(newForm);
344-
$newLinkLi.before($newFormLi);
342+
// Add the new form at the end of the list
343+
$collectionHolder.append($newFormLi)
345344
}
346345
347346
.. note::

reference/forms/types/options/choice_attr.rst.inc

+14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ If an array, the keys of the ``choices`` array must be used as keys::
1313
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
1414
// ...
1515

16+
$builder->add('fruits', ChoiceType::class, [
17+
'choices' => [
18+
'Apple' => 1,
19+
'Banana' => 2,
20+
'Durian' => 3,
21+
],
22+
'choice_attr' => [
23+
'Apple' => ['data-color' => 'Red'],
24+
'Banana' => ['data-color' => 'Yellow'],
25+
'Durian' => ['data-color' => 'Green'],
26+
],
27+
]);
28+
29+
// or use a callable
1630
$builder->add('attending', ChoiceType::class, [
1731
'choices' => [
1832
'Yes' => true,

0 commit comments

Comments
 (0)