2

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

Leszek
  • 182
  • 1
  • 14

1 Answers1

1

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?

  • But as you can see above I've already set mapped as false. Entities is very simple many to many relationship (I edited question) – Leszek Nov 05 '19 at 09:12
  • In construct did you initialized the properties to be ArrayCollection? $this->tags = new ArrayCollection(); –  Nov 06 '19 at 10:07
  • Of course, basically entities was generated by Maker Bundle I don't think that entities are problem here – Leszek Nov 07 '19 at 07:26