Symfony 4 + Select 2 - add more options to entityType

743 Views Asked by At

I'm working on app based on Symfony 4 with Select2 library.

In my src/Form/PostType.php file I declared field tag, where user should be able to set one of predeclared Tag or add new one (by type tag name and press enter).

$builder
->add('tags', EntityType::class, [
    'class' => Tag::class,
    'choice_label' => 'name',
    'mapped' => false,
    'expanded' => false,
    'multiple' => true,
    'required' => false,
    ]);

From the frontend side I'm using select2 library to handle with displaying tags field.

In below example fist tag was chosen from the existed entity in database, the second one should be saved in this second.

enter image description here

Any idea what should I changed into filed declaration to make this field valid also for new tags? Controller is ready, only issue is to pass form validation :)

EDIT:

Relations in ORM looks like this:

class Company {

/**
 * @ORM\ManyToMany(targetEntity="App\Entity\Tag", mappedBy="companies")
 */
private $tags;

}

class Tag
{
/**
 * @ORM\ManyToMany(targetEntity="App\Entity\Company", inversedBy="tags")
 */
private $companies;

}

and there is no other validation than in code above

1

There are 1 best solutions below

3
On

You have set the field to be mapped = false. If a field is unmapped you have to handle form validation manually. Can you share your Entities code , any validation code if it's written?