The "How to Embed a Collection of Forms" takes an example but does not really cover the case.
It's about task and their associated tags. ManyToMany association. It shows how to add or remove tags from a task, but the case is much more interesting: tags are reusable, and should be reused. The form should show the user a list of existing tags, of which he could pick some and add them to the collection, as well as letting him create new ones.
The "add an existing tag to the collection" isn't covered and looks tricky (to me) as it requires different validation than the adding process.
Simple workaround may be proposed but they won't fit to bigger projects, where a real handling of collection addition of existing entities should be used.
Hope i was clear in my explanations, if not, let me know.
This is a bit harder, which is why it might be interesting :). I see it as a new article that builds off of the same foundation of the form_collections article, but with the reusing of existing tags that you've mentioned. I believe it would require a number of more advanced concepts.
Is there any documentation about this subject? Want to create and add existing tags inside same page but the docs don't provide an solution. Cant find anything about this on google as well.....
@coenswaans the advanced article of which this issue is talking is not yet created. However, you can take a look at http://symfony.com/doc/current/cookbook/form/form_collections.html
@WouterJ the cookbook does not show how to add existing tags to a task, only add new (allow_add) and remove tags from task. To prevent duplicate tags i would like to be able to select as well as add tags (like @NinjDS first post).
Do you know of any tutorials or documentation to point me in the right direction?
I'm looking for the same tutorial in order to do things correctly. Did you finally find anything ?
+1
I 'm going Knots!! I have the same problem please help me! Does anybody has the answer?
Ditto. Such a theoretically simple thing seems to be exceptionally difficult, particularly when it involves moving items between collections, without creating duplicates or deleting existing items.
I check it in the controller. First I get all the tags from the database, then compare with the submitted tags and replace if a certain tag already exists.
_ProductionAdditionFormType_
->add('tags', 'collection', array(
'type' => new TagType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
))
_RecordAdditionController_
if($productionAdditionForm->isValid()) {
$production = $productionAdditionForm->getData();
$formTags = $production->getTags();
$existingTags = $em->getRepository('AppBundle:Tag')->findAll();
/* @var $formTag Tag
* @var $existingTag Tag */
foreach($formTags as &$formTag) {
foreach($existingTags as $existingTag) {
if($formTag->getName() === $existingTag->getName()) {
$production->removeTag($formTag);
$production->addTag($existingTag);
}
}
}
$em->persist($production);
$em->flush();
return $ajaxManager->getResponseForFormSubmission($productionAdditionForm, true);
}
_record_addition.html.twig_
<fieldset>
<legend>tags</legend>
<ul class="tags" data-prototype="{{ form_widget(production_addition_form.tags.vars.prototype)|e }}">
{% for tag in production_addition_form.tags %}
<li>{{ form_row(tag.name) }}{{ form_row(tag.genre) }}</li>
{% endfor %}
</ul>
</fieldset>
{% do production_addition_form.tags.setRendered %}
+1
+1
Here's my solution with a custom DataMapper decorator.
https://github.com/vudaltsov/symfony-docs-add-existing-tags
Existing tags are chosen with entity type field.
For new tags we create a mapped=false collection field and then map them manually with custom DataMapper.
@vudaltsov This hyperlink no longer works and this issue is still a thing, could you please show me where this solution was moved?
@butteredptarmigan , I am sorry, I deleted the repository, because it didn't arouse any interest at the time I created it. Since then I stopped using forms in favor of API-based solutions, so I am not able to recall the solution.
@vudaltsov No problem, I have already found a brilliant solution by Jules Pietri