Alsatian67 / FormBundle

Bunch of FormTypes extending some basic Symfony PHP framework FormTypes. ExtensibleTypes makes it possible to allow extra choices by Symfony ChoiceTypes (ChoiceType, EntityType and DocumentType). This can help to make it work with the Select2 jQuery pluggin.
MIT License
14 stars 6 forks source link

Problem with select2 dynamic option creation feature #18

Closed Namalee closed 4 years ago

Namalee commented 4 years ago

I have the following problem : in my form, i need to have a select2 field that allow the user to search for existing items but can also allow to type in a new one. When i use this bundle only to search for existing items it works perfectly but when i type in a non existing item (to create it) i have the following error during form submission :

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

The problem comes from the fact that when you type in a brand new value, the select2 does not return the id but the typed text and when you try to get the datas from the database, an error is raised because we used a string on an id which is an int value.

Alsatian67 commented 4 years ago

This is by design on the EntityType (Submitted data is converted to entity, submitted existing entities are chosen). ExtensibleEntityType extends the EntityType, and I cannot change this behavior.

You should use the ExtensibleChoiceType instead of the ExtensibleEntityType :

ExtensibleChoiceType accept everything that has been submitted. Then you can configure your own business logic to search the submitted choices, and create an entity if you can't find one.