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"
/**
*
* @ORM\Column(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'
));
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.
Why don't you set the role in the controller directly like this ?
$user->setRoles(['ROLE_PARTICULAR']);