Expected argument of type "array", "string" given at property path "roles". in Symfony

  arrays, string, symfony, symfony-forms, symfony5

I’m just trying to add a field hidden in a form by passing a value by default to the field “Roles” in symfony.
I check multiple tutoriel but I did’nt find exactly what I am looking for.

I put how i save the field “roles” in my database, i put how the field “Roles” is declared in my entity and also i put how i try to send the data by hidden in the form.

This is my UsersEntity where i save the “roles”

 /**
 *
 * @ORMColumn(name="roles", type="array", nullable=false)
 */
private $roles = [];

public function getRoles(): ?array
{
    $roles = $this->roles;
    $roles[] = 'ROLE_USER';
    return array_unique($roles);
}

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

    return $this;
}

This is my RegistrationFormType

$builder
        ->add('email')
        ->add('username')
        ->add('lastName')
        ->add( 'firstName')
        ->add('address')
        ->add( 'phone')
        ->add( 'city')
        ->add('postalCode')
        ->add('roles', HiddenType::class, array(
            'data' => 'ROLE_PARTICULAR'
        ));

This is the error

I can’t pass an array, he tells me every time I pass him a string. I tried several syntaxes, I can’t do it, can someone help me please? I’m new to Symfony 5
Thank you for your help.

Source: Symfony Questions

2 thoughts on - Expected argument of type "array", "string" given at property path "roles". in Symfony

LEAVE A COMMENT