[Form] AbstractChoiceLoader
can return invalid results
#44655
Labels
8000
AbstractChoiceLoader
can return invalid results
#44655
Symfony version(s) affected
5.1.0, 5.4-dev, 6.1-dev
Description
I've discovered a design issue with
AbstractChoiceLoader
which manifested itself in a form failing to recognise a valid submitted value.In our application this causes problems when the (new in 5.1)
choice_filter
option is used with theexpanded => true
option, initially I thought this was something specific to us (we actually had achoice_filter
option on our type before Symfony added one) but it seems this combination are not used by many/any people as it just flat out doesn't work.The root cause I believe is the following code:
symfony/src/Symfony/Component/Form/ChoiceList/Loader/AbstractChoiceLoader.php
Lines 34 to 37 in 9870e7e
Observe how the
ArrayChoiceList
is stored in a property without regard for the supplied$value
callback. It appears that$value
is sometimes called withnull
or the value of thechoice_filter
option which results in inconsistent data.How to reproduce
This code demonstrates the problem if run from the root directory of a Symfony checkout.
Again a more representative example of the problem:
Possible Solution
This is one possible solution, with the downside that
ArrayChoiceList
is going to be constructed more frequently.Another option would be storing the
ArrayChoiceList
indexed by the callback. Or more involved add an < 8000 code class="notranslate">ArrayChoiceList::withValue() method allowing the values to be overridden without a rebuild of the whole object.Seems this simple change fixes the
ChoiceType
error but doesn't address the underlying issue withinAbstractChoiceLoader
.Additional Context
No response
The text was updated successfully, but these errors were encountered: