Fix ModelChoiceList for Models with required=false and empty value selected #450
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Using Symfony 2.7+ I cannot select empty values using ModelChoiceList with required=false.
I have a field like this :
I get :
SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: ""
Looking at the code and especially at the backtrace I have :
An empty choice value seems to be reverseTransformed to array('') and then passed to ModelChoiceList::getChoicesForValues which doesn't check for empty values in an array and starts the query (find()).
If I remember well in Symfony <= 2.5, empty values were removed before applying reverse transforms thus this problem didn't appear (I am getting this while upgrading a Symfony 2.5 based project).
If empty values get now queried as array('') then ModelChoiceList should take care of taking them into account (look at ChoiceToValueTransformer:46
$choices = $this->choiceList->getChoicesForValues(array((string) $value));
)This patch fixes exactly this use case but a probably better way of doing this would be to remove all empty values from the input array before querying the database.