I have simple many-to-many relation between Article
and Tag
entities. I want to create a new article using FormType and associate tags with it. But the case is: I want to associate tags that may not exist yet.
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('title', TextType::class)
->add('tags', EntityType::class, [
'class' => Tag::class,
'multiple' => true
])
;
}
This FormType generates a multi select form for existing tags only. But I want to have a <textarea>
field, where users can put existing and not existing tags. Then after form submission, existing tags would be associated with the new article, and not existing tags first would be added and then associated with a new article.
I’m pretty new in Symfony world, so excuse me if my problem is trivial.
2
Answers
First, sorry for my English. I will give you an example based on
Embarazada
andEtiquetaAspectoEmbarazada
entities (Many to many relationship) and the use of tetranz /select2entity-bundle The idea of that is register an pregnant woman and asociate her many tags, in case of entered text a tag name that not exist, it could be inserted from the Embarazada form type.EmbarazadaType form class sumarized:
The logic of the action captarAction (register pregnam woman) look for the comments inside:
Check the final result (red frame):
You should use several tricks to reach it.
choices
all existing tags but as a relationArticleHasTag
.I can show you how it works.
look at
choices
option and submit the fragment here.of course, you also should make entities.