Symfony – ChoiceType Form – Array to string conversion error

Symfony – ChoiceType Form – Array to string conversion error

I’m trying to make a form with a choice type input on the field roles on my entity user.
However, Symfony reply me an error “Array to string conversion error”

My roles don’t have to be an array because the are not multiple (role hierarchy in security.yaml).

Here is my Form Builder :

 $builder
        ->add('email')
        ->add('prenom')
        ->add('nom')
        ->add('password', PasswordType::Class)
        ->add('confirm_password', PasswordType::Class)
        ->add('roles', ChoiceType::class, [
            'choices'  => [
                'Utilisateur' => "ROLE_USER",
                'Valideur' => "ROLE_VALIDEUR",
                'Administrateur' => "ROLE_ADMIN",                    
            ],

        ])

And my User entity :

class User implements UserInterface
{
/**
 * @ORMId()
 * @ORMGeneratedValue()
 * @ORMColumn(type="integer")
 */
private $id;

/**
 * @ORMColumn(type="string", length=255)
 * @AssertLength(min="8", minMessage="Votre mot de passe doit contenir au moins 8 carractères")
 */
private $password;

/**
 * @AssertEqualTo(propertyPath="password", message="Vous n'avez pas tapé le même mot de passe")
 */
private $confirm_password;

/**
 * @ORMColumn(type="simple_array")
 */
private $roles = array();

/**
 * @ORMColumn(type="string", length=255)
 * @AssertNotBlank
 */
private $prenom;

/**
 * @ORMColumn(type="string", length=255)
 * @AssertNotBlank
 */
private $nom;

/**
 * @ORMColumn(type="string", length=255)
 * @AssertEmail()
 * @AssertNotBlank
 */
private $email;



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

public function getPassword(): ?string
{
    return $this->password;
}

public function setPassword(string $password): self
{
    $this->password = $password;

    return $this;
}

public function getConfirmPassword(): ?string
{
    return $this->confirm_password;
}

public function setConfirmPassword(string $confirm_password): self
{
    $this->confirm_password = $confirm_password;

    return $this;
}

public function getRoles(): array
{
    return array_unique($this->roles);
}

public function setRoles(array $roles): self
{
    $this->roles = $roles;

    return $this;
}

public function getPrenom(): ?string
{
    return $this->prenom;
}

public function setPrenom(?string $prenom): self
{
    $this->prenom = $prenom;

    return $this;
}

public function getNom(): ?string
{
    return $this->nom;
}

public function setNom(?string $nom): self
{
    $this->nom = $nom;

    return $this;
}

public function getEmail(): ?string
{
    return $this->email;
}

public function setEmail(string $email): self
{
    $this->email = $email;

    return $this;
} 

public function getUsername(): ?string
{
    return $this->email;
}

public function eraseCredentials()
{
}

public function getSalt()
{
}
}

Could you explain me where are the problems in my user Entity (or somewhere else) please ?

Source: Symfony Questions

Leave a Reply

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