Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 and Selectize.js: Clearest way to persist new items in entity field type?

In Symfony2 I have BandType, where I add the entity Tag:

->add('tags', 'entity', [
     'label' => 'Tags',
     'class' => 'DbBundle:Tag',
     'property' => 'title',
     'multiple'  =>  true,
])

This generate multiple select element, where I can choose existing tags from database (Doctrine). But I need to add new tags dynamicaly, which don't exist yet.

On a client side I use jQuery plugin Selectize.js, which allows me to add new tag to select box. But after submit form the new tags are not saved.

So my question is - what is the clearest way to persist new items from select box (entity field type)?

like image 327
Tomas S. Avatar asked Apr 23 '15 16:04

Tomas S.


1 Answers

Use a Data Transformer for your entity. And in the reverseTransform method, if you don't find the newly added band, simply create it there instead of throwing a TransformationFailedException.

like image 52
Stev Avatar answered Sep 20 '22 10:09

Stev