Skip to content
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

Fix ModelChoiceList for Models with required=false and empty value selected #450

Merged
merged 1 commit into from Nov 21, 2022
Merged

Conversation

sberthelot
Copy link

Using Symfony 2.7+ I cannot select empty values using ModelChoiceList with required=false.

I have a field like this :

$builder->add('field', 'Propel\Bundle\PropelBundle\Form\Type\ModelType', array(
                'class' => 'MyBundle\Model\Field',
                'property' => 'name',
                'query' => $customQuery,
                'choices_as_values' => true,
                'mapped' => true,
                'required' => false,
        )

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 :

at ModelCriteria->find()
        in _path_/vendor/propel/propel-bundle/Form/ChoiceList/ModelChoiceList.php line 204

    at Propel\Bundle\PropelBundle\Form\ChoiceList\ModelChoiceList->getChoicesForValues(array(''))
        in _path_/vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/LegacyChoiceListAdapter.php line 109

    at Symfony\Component\Form\ChoiceList\LegacyChoiceListAdapter->getChoicesForValues(array(''))
        in _path_/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoiceToValueTransformer.php line 46

    at Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToValueTransformer->reverseTransform('')
        in _path_/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 1182

    at Symfony\Component\Form\Form->viewToNorm('')
        in _path_/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 627

    at Symfony\Component\Form\Form->submit('', true)
        in _path_/vendor/symfony/symfony/src/Symfony/Component/Form/Form.php line 567

    at Symfony\Component\Form\Form->submit(array('field' => ''', 'submit' => '', [/other data/]), true)
        in _path_/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandle

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.

@dereuromark
Copy link
Contributor

Is there any approval/review on this? Then we can merge.

@dereuromark dereuromark merged commit bfc6ad1 into propelorm:1.5 Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants