Skip to content

[form] error when submitting empty value in for using model choice list #14583

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

Closed
piogrek opened this issue May 7, 2015 · 10 comments
Closed

Comments

@piogrek
Copy link

piogrek commented May 7, 2015

I think that's happening only on propel + postgres after @webmozart removed empty/null checks from ChoiceToValueTransformer in 1d89922

ModelChoiceList->getChoicesForValues allows to create Propel query with empty string values which are not accepted, so I'm getting:

Unable to execute SELECT statement [SELECT (...) FROM twilio_number WHERE twilio_number.id IN (:p1)] [wrapped: SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: ""]

the code below (for propel bridge) seems to do the trick, but I'm not sure if I can create PR for the Propel1Bridge repository. Will appreciate any help.

class ModelChoiceList extends ObjectChoiceList

    public function getChoicesForValues(array $values)
    {
        if (empty($values) || empty($first = reset($values))) {
            return array();
        }
@Tobion
Copy link
Contributor

Tobion commented May 8, 2015

Fixed in #14465 I guess.

@Tobion Tobion closed this as completed May 8, 2015
@piogrek
Copy link
Author

piogrek commented May 8, 2015

It was fixed partially. but in our case exception happens in ModelChoiceList , before the null/empty check in ChoiceToValueTransformer.

still not fixed, but I can't re-open this ticket now

@Tobion Tobion reopened this May 8, 2015
@mbartok
Copy link

mbartok commented May 10, 2015

Same error after submitting optional entity form field type with empty_value with doctrine on Symfony 2.7 (e60f715). Related issues are #14393 and #14465, but non of those fixes seems to fix this issue.

Field definition:

$builder->add('dealer', 'entity', array(
    'label' => 'useraccount.label.dealer',
    'class' => 'MyAppBundle:Subject',
    'choice_label' => 'label',
    'empty_value' => '',
    'multiple' => false,
    'required' => false
));

After submitting empty value i get this error:

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: ""

It seems like Symfony is trying to find Subject entity using empty string as id for dealer field:

DBALException: An exception occurred while executing 'SELECT ... FROM subject s0_ WHERE s0_.id IN (?)' with params [""]:

@piogrek
Copy link
Author

piogrek commented May 10, 2015

Seems to be the same problem, but fix needs to be done either in ChoiceToValueTransformer (for both model and entity lists) or separately in bridge ChoiceList classes

@jakzal jakzal added the Form label May 12, 2015
phansys added a commit to phansys/symfony that referenced this issue May 12, 2015
| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | symfony#14583
| License       | MIT
| Doc PR        |

Fixes symfony#14583.
@madmis
Copy link

madmis commented May 29, 2015

+1
ChoiceToValueTransformer
Form

$builder->add('country', 'entity', [
            'class' => 'AppBundle\Entity\Country',
            'query_builder' => function (EntityRepository $er) {
                return $er->createQueryBuilder('c')
                    ->orderBy('c.name', 'ASC');
            },
        ]);

If in this case i try save empty value, i have error

An exception occurred while executing 'SELECT c0_.id AS id0, c0_.iso_code AS iso_code1, c0_.name AS name2, c0_.slug AS slug3 FROM country c0_ WHERE c0_.id IN (?) ORDER BY c0_.name ASC' with params [""]:

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: ""

If set 'empty_data' => 0,

$builder->add('country', 'entity', [
            'class' => 'AppBundle\Entity\Country',
            'empty_data' => 0,
            'query_builder' => function (EntityRepository $er) {
                return $er->createQueryBuilder('c')
                    ->orderBy('c.name', 'ASC');
            },
        ]);

worked without errors

Symfony version 2.7.0-DEV

@tmeliberty
Copy link

@madmis by trying this workaround I get an TransformationFailureException "The choice "0" does not exist or is not unique" and it break my form validation

Symfony 2.7.0 LTS

@althaus
Copy link
Contributor

althaus commented Jun 1, 2015

This happens for me with the placeholder option:

$builder->add('category', null, array(
            'label' => _('Category'),
            'order_by' => 'title',
            'placeholder' => _('none'),
        ));

@webmozart
Copy link
Contributor

This bug is fixed for Doctrine in #14950.

@althaus
Copy link
Contributor

althaus commented Jun 12, 2015

That fix solves my issue... although it feels somehow hacky to just filter any non-integers from the values. Shouldn't it be fixed somehow related to empty_data as this is the cause of the empty string?

@garak
Copy link
Contributor

garak commented Feb 3, 2016

I just found this issue since I'm getting exactly this problem, but with guid type. I think that guid should be added to the array of types to check.
So, instead of array('integer', 'bigint', 'smallint'), it should be array('integer', 'bigint', 'smallint', 'guid')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests