-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Form] [Form Theme] customize choice entry #15921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
e95f547
69d06ff
7608bb8
9341724
7f5decf
944172c
ca777f2
d87f890
f5acd32
c9118fb
f7f46d4
444cff6
45fab1b
b400cf8
1c7efeb
eaa2fc7
2d26466
25d7f17
25b24d8
65ebe2d
7de48b8
8ee0c64
4ce9083
6f201de
4d5b3ad
b051054
0dbd299
d3cb494
0446c44
c24bf0d
425563d
c08a0a8
664bc4b
a385ac1
1b4557c
23d8fc6
91b25d7
02ae0a5
875662d
c89c883
70d88d7
852aa11
b63ccf7
d6e5067
24b9b1d
01714e1
d48ec7c
bd50806
2b1e48a
539c992
5222e73
a0ee1ec
a1eb2f2
480c2b5
1af5df1
ac30623
31fd54a
d2839f1
6d84eef
48b96a2
0d8bd19
d30127a
f419727
ae01b32
262c538
d893d0b
d8cae8c
3a35bda
fede87d
10f4b6e
6798956
725d856
cd5c197
36dc4e3
7b9406e
8d03546
ccd76b1
67ab35a
f20f6a2
422bb7f
ab52db0
31c246f
3e1074c
2875b63
13999a2
2298caa
080e516
6f72b56
9ff5eab
49d4e10
dbf5802
dabd9ce
4c55db3
26a7fab
b54b7ee
ef0dc2b
b07740b
e7e2c2e
1c944b4
1c2a61a
704602d
5773540
5076e42
3c0734a
98a717a
45a86a3
5611f88
347f749
1e071d5
626bb8c
e7c4ead
dcce2f9
f5efd90
63c2a8d
91d5f05
994136d
5857093
5823947
c405efa
8c443c6
1b6789f
835a8e0
55078f3
5555a54
48f0d4f
62a65a6
967e7a4
6c90946
c1fbe13
955b393
d71b5bc
5781a94
41f4013
e45d89e
1f8bd5b
6f431a4
c4c1899
dad267e
b636d2e
afb49b8
942fd56
1898849
5947b3e
2bbc0c9
038872a
5c2240b
2309b0b
f7948d8
5622342
c49360d
aa5f86b
b10cf9a
74daa0e
4f90b3c
4648d02
117ed8f
c08e8fd
a7c7378
61cd54d
762f73b
22217fa
69f5546
03ecb46
b939e39
220aeb0
e05e507
4238c6d
e5dbbdc
b4dddfd
5a02dc1
cb957d7
0d3401e
be6992c
b67d08b
ab9dfa4
1544a18
800017d
85ad399
e5c3fd6
fd78913
8112798
43f8662
157020f
4cf014e
c845048
79e5249
deb47da
ea9a3f7
b8900be
6bac4e0
76eeecb
ac46df4
90ce7e6
c95dbb1
9400c0f
4374617
2c26d77
87b1a79
c0e8d75
8bf5f9e
1f3ebe6
ef07e1a
978bbe6
7e2ae9a
3b4b287
4c6a373
576e20a
7f426b3
550d992
793c3cb
afe07ab
5eaa22b
7f8c5f8
9762db6
5c68371
ef39db7
0a258db
ad86890
5dbace5
b6bcc3d
5029cb2
b532f37
0c7f1ae
4d24ada
e9137e0
73aa358
9366767
e7e142a
7dcd023
44fe3b2
c5bad2c
0645727
7ad5d9f
f83e1d3
885e9c3
511d460
41c05cd
34881a8
b072fe9
de43ff1
3e417bf
4d22728
1f24506
f1c2056
a1c3065
262baba
ebbffc5
eca3116
e29a873
6705a60
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -371,6 +371,65 @@ by ``row``, ``widget``, ``label``, or ``help``): | |||||
{# the field of TaskType #} | ||||||
{% endblock %} | ||||||
|
||||||
Access data in a Form Choice | ||||||
............................. | ||||||
|
||||||
When you have a ChoiceType or an EntityType, and you want to customize the ``entry``, row or widget, you can also retrieve | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Then |
||||||
the data of the choice, but not directly in the ``form.vars.data`` of the child. See an exemple:: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could improve this sentence altogether by defining what is actually the entry data, before suggesting to use When you use an expanded ``ChoiceType`` and need to customize the children ``entry`` blocks,
the ``form.vars`` of entries (radio button or checkbox) may not be enough since each holds a
boolean value meaning whether a choice is selected or not.
To get the full list of choices data and values, you will need to access the ``choices`` variable
from their parent form (the ``ChoiceType`` itself) with ``form.parent.vars.choices``: |
||||||
|
||||||
class TaskManagerType extends AbstractType | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By moving this section as suggested above, adding one more PHP example should not be necessary in that context, a reference to https://symfony.com/doc/4.4/reference/forms/types/choice.html#advanced-example-with-objects could be enough as it is also used to illustrate most of the options in that page. |
||||||
{ | ||||||
public function buildForm(FormBuilderInterface $builder, array $options = []): void | ||||||
{ | ||||||
// $tasks = [ | ||||||
// new Task('one', 'blue', 'open'), // id: 1 | ||||||
// new Task('two', 'red', 'closed'), // id: 2 | ||||||
// ]; | ||||||
|
||||||
$builder->add('taskLists', EntityType::class, [ | ||||||
'class' => Task::class, | ||||||
'multiple' => true, | ||||||
'expanded' => true, | ||||||
'choice_label' => function (Task $task) { | ||||||
return $task->getName(); | ||||||
} | ||||||
]); | ||||||
} | ||||||
} | ||||||
|
||||||
Now we want to display each checkbox inside a table element. The entity is not accessible in ``form.vars.value`` nor ``form.vars.data``. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another alternative for the following paragraphs could be: Given the advanced object example, each entry would have access to the following variables:
.. code-block:: html+twig
{# `true` or `false`, whether the current choice is selected as radio or checkbox #}
{{ form.vars.data }}
{# the current choice value (i.e a category name when `'choice_value' => 'name'` #}
{{ form.vars.value }}
{# a map of `ChoiceView` or `ChoiceGroupView` instances indexed by choice values or group names #}
{{ form.parent.vars.choices }} |
||||||
In the example above, if we dump ``form.vars.value`` we can see ``"1"`` and ``form.vars.data`` = ``false``. | ||||||
|
||||||
Because the value is "1", in case of EntityType, is in reality the ``id`` of the entity, and data is the value of the checkbox, | ||||||
and the checkbox is unchecked by default. | ||||||
|
||||||
So the real data is inside ``form.parent.vars.choices[key].data``, because the parent knows all the choices, and for each | ||||||
``ChoiceView``` we have: | ||||||
|
||||||
.. code-block:: text | ||||||
|
||||||
// form.parent.vars.choices | ||||||
choices = [ | ||||||
1 => ChoiceView(label: 'one', value: '1', data: 'a Task Entity', other_fields...), | ||||||
2 => ChoiceView(label: 'two', value: '2', data: 'a Task Entity', other_fields...), | ||||||
] | ||||||
|
||||||
Get the entity from the array of choices and use the key of the child: | ||||||
|
||||||
.. code-block:: html+twig | ||||||
|
||||||
{% block _task_manager_taskLists_entry_widget %} | ||||||
{% set entity = form.parent.vars.choices[form.vars.value].data %} | ||||||
|
||||||
<tr> | ||||||
<td>{{ form_widget(form) }}</td> | ||||||
<td>{{ form.vars.label }}</td> | ||||||
<td> | ||||||
{{ entity.name }} | {{ entity.color }} | {{ entity.status }} | ||||||
</td> | ||||||
</tr> | ||||||
{% endblock %} | ||||||
|
||||||
Template Fragment Inheritance | ||||||
............................. | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This section here is breaking the flow of this form themes page IMHO.
Maybe we should have it at the end of the ChoiceType reference page with a link from here?