Symfony how to insert into a form a type Array | IteratorAggregate?

In symfony I have a ManyToOne relation :

Each Article has One Category.
Each Category may have Many Article.

Here are my two entities :

Article

/**
 * @ORMId()
 * @ORMGeneratedValue()
 * @ORMColumn(type="integer")
 */
private $id;

/**
 * @ORMColumn(type="text")
 */
private $title;

/**
 * @ORMColumn(type="text")
 */
private $content;

/**
 * @ORMColumn(type="datetime")
 */
private $createdAt;

/**
 * @ORMColumn(type="string", nullable=true)
 */
private $image;

/**
 * @ORMColumn(type="datetime", nullable=true)
 */
private $editedAt;

/**
 * @ORMManyToOne(targetEntity="AppEntityCategory")
 */
private $category;

public function getId(): ?int
{
    return $this->id;
}

public function getTitle(): ?string
{
    return $this->title;
}

public function setTitle(string $title): self
{
    $this->title = $title;

    return $this;
}

public function getContent(): ?string
{
    return $this->content;
}

public function setContent(string $content): self
{
    $this->content = $content;

    return $this;
}

public function getCreatedAt(): ?DateTimeInterface
{
    return $this->createdAt;
}

public function setCreatedAt(DateTimeInterface $createdAt): self
{
    $this->createdAt = $createdAt;

    return $this;
}

public function getImage(): ?string
{
    return $this->image;
}

public function setImage(?string $image): self
{
    $this->image = $image;

    return $this;
}

public function getEditedAt(): ?DateTimeInterface
{
    return $this->editedAt;
}

public function setEditedAt(?DateTimeInterface $editedAt): self
{
    $this->editedAt = $editedAt;

    return $this;
}

public function getCategory(): ?Category
{
    return $this->category;
}

public function setCategory(?Category $category): self
{
    $this->category = $category;

        return $this;
}

Category

/**
 * @ORMId()
 * @ORMGeneratedValue()
 * @ORMColumn(type="integer")
 */
private $id;

/**
 * @ORMColumn(type="string", length=255)
 * @AssertUnique(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 mixed
 */
public function setName($name)
{
    return $this->name = $name;
}

I have a basic form to add new category :

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', TextType::class)
        ->add('submit', SubmitType::class, [
            'label' => 'Add a new category'
        ])
    ;
}

The controller that handle the request :

public function addCategory(Request $request)
{
    $category = new Category();
    $form = $this->createForm(CategoryType::class, $category);
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $category->setName($request->request->get('category_add')['name']);
        $em = $this->getDoctrine()->getManager();
        $em->persist($category);
        $em->flush();
    }
    return $this->render('admin/category.html.twig', [
        'form' => $form->createView(),
    ]);
}

The problem is that when I try to submit the form with any value in the Name input it returns : This value should be of type array|IteratorAggregate.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *