I have 2 entities:
Category and Article with the following relation:
- Each
Articlehas OneCategory. - Each
Categorymay have ManyArticle.
The problem :
I want to add new category using this form :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class)
->add('submit', SubmitType::class, [
'label' => 'Add a new category'
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Category::class,
]);
}
Here's the error I get when I enter a string value in the field name:
Name Error This value should be of type array|IteratorAggregate.
I've never used ArrayCollection / Relation in symfony but I've tried to change the TextType::class to CollectionType::class it gives me a white square and I can't type anything.
In a more generic question :
How could I validate values in a form without having the error : This value should be of type array|IteratorAggregate.
Category
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Unique(message="Cette catégorie existe déjà")
*/
private $name;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param $name
* @return Collection|null
*/
public function setName($name): ?Collection
{
return $this->name = $name;
}
Article
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="text")
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $content;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $image;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $editedAt;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category")
*/
private $category;