SkroS2
SkroS2

Reputation: 81

Symfony 4 - EntityType - Load current value

I am using an EntityType in a formType in Symfony 4. When I am displayed it in a new form all works great, the choicetype liste contain all the values from my database. But when I load existing data from my database the choice list don't load the value from the database, it display the placeholer. I have checked in the database and the value is saved. My form looks like this

->add('operatingSystem', EntityType::class, [
                 'class' => "App\Entity\TicketOperatingSystem",
                 'choice_label' => 'name',
                 'label'    => 'Système d\'exploitation',
                 'required' => false,
             ])

The entity :

/**
 * @ORM\ManyToOne(targetEntity="Maps_red\TicketingBundle\Entity\TicketOperatingSystem")
 * @ORM\JoinColumn(name="operating_system", referencedColumnName="id", nullable=true)
 */
private $operatingSystem;

public function getOperatingSystem(): ?TicketOperatingSystemInterface
{
    return $this->operatingSystem;
}

public function setOperatingSystem(?TicketOperatingSystemInterface $ticketOperatingSystem): UserInstance
{
    $this->operatingSystem = $ticketOperatingSystem;

    return $this;
}

OperatingSystem Entity :

/**
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

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

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

public function getName(): ?string
{
    return $this->name;
}

public function setName(string $name): TicketOperatingSystemInterface
{
    $this->name = $name;

    return $this;
}

For testing I try to change the EntityType to a ChoiceType with a list of 2, it works perfectly. I don't know what the problem with EntityType is.

Upvotes: 0

Views: 76

Answers (1)

Manzolo
Manzolo

Reputation: 1959

Try adding 'choice_value' => 'name',

  ->add('operatingSystem', EntityType::class, [
             'class' => "App\Entity\TicketOperatingSystem",
             'choice_label' => 'name',
             'choice_value' => 'name',
             'label'    => 'Système d\'exploitation',
             'required' => false,
         ])

Upvotes: 1

Related Questions