0

So I'm trying to create an input array for my form, but haven't quite found any resource in the docs for this (I could have missed something so link if the case).

In short, I'd like to render something like this:

<select name="my-select" id="some-select">
    <option value="a">A</option>
    <option value="b">B</option>
    <option value="c">C</option>
</select>

<button type="button" class="btn btn-primary" id="add-new-option">
    Add Another Option
</button>

so the idea is user can select Option A, hit the button to add Option C as well as A to the entity.

The option results get stored in a table like this:

entity_id  option_value
1          A
1          C
2          B
2          A

I thought of just doing it like this:

foreach ($arrayInput as $key => $value)
{
    $option = new Option();
    $option->setEntityId($entity->getId());
    $option->setOptionValue($value);
}

in the controller.

But I have a horrible feeling there's already a way to do this, I just haven't been able to find it. Otherwise I have to use HTML in my twig templates and I'm trying to keep that reduced..

So is the above method the best way to achieve what I want?

My current entity:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\PageRepository")
 */
class Page
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=125)
     */
    private $title;

    /**
     * @ORM\Column(type="text")
     */
    private $content;

    /**
     * @ORM\Column(type="boolean")
     */
    private $approved;

    /**
     * @ORM\Column(type="datetime")
     */
    private $created_ts;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $last_edit_ts;

    /**
     * @ORM\Column(type="integer")
     */
    private $author_id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Tag")
     */
    private $tag;

    public function getTag(): ?Tag
    {
        return $this->tag;
    }

    public function getId()
    {
        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 getApproved(): ?bool
    {
        return $this->approved;
    }

    public function setApproved(bool $approved): self
    {
        $this->approved = $approved;

        return $this;
    }

    public function getCreatedTs(): ?\DateTimeInterface
    {
        return $this->created_ts;
    }

    public function setCreatedTs(\DateTimeInterface $created_ts): self
    {
        $this->created_ts = $created_ts;

        return $this;
    }

    public function getLastEditTs(): ?\DateTimeInterface
    {
        return $this->last_edit_ts;
    }

    public function setLastEditTs(?\DateTimeInterface $last_edit_ts): self
    {
        $this->last_edit_ts = $last_edit_ts;

        return $this;
    }

    public function getAuthorId(): ?int
    {
        return $this->author_id;
    }

    public function setAuthorId(int $author_id): self
    {
        $this->author_id = $author_id;

        return $this;
    }
}

Thanks

4
  • The best way is not to mix forms with entities. Btw, any particular reason for using the snake-case notation for entity properties? Commented May 30, 2018 at 9:36
  • @emix really? They have an EntityType - would that not be counter-intuative if they didn't want to mix entities with forms? And I generated the class with make:entity and to me, database field should be snake_case rather than camelCase Commented May 30, 2018 at 9:49
  • 1) They have EntityType so less techy people can prototype more quickly, I don't wish to discuss this further. Just because a Singleton pattern exists, that doesn't mean you should use it 2) Database properties sure, but not entity properties Commented May 30, 2018 at 9:51
  • @emix 1) that's fair enough, Magento has similar methods that are there but should be avoided. 2) but I used make:entity so it auto gens my class properties from the database properties Commented May 30, 2018 at 9:56

1 Answer 1

1

You could use ChoiceType (for strings) or EntityType (for entities) with 'multiple' => true.

Sign up to request clarification or add additional context in comments.

2 Comments

that half works, but how do I then use the result array to post to a different table from the entity?
I need more information to help you. Can you post your entity?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.