I need to modify a form field attribute to have it disabled when a user has a specific User role.
I saw a Question in which the asker did something similar to:
->add('description')
if($user.hasRole(ROLE_SUPERADMIN))
->add('createdAt')
This would suffice for me as I only need to do this once in the whole Type, but I can't use an if-statement in the form-builder. Is there are way to be able to modify the attributes when a user has that specific user-role?
The part I want to modify is the cashbackThreshold field. Also, this is part of a continuous form type and I can't put it in a different form type
//Payments panel
$builder->create('payments', 'form', array('virtual' => true, 'attr' => array('class' => 'form-section')))
->add('commission', 'integer')
->add('cashbackThreshold', 'integer')
EDIT
I have found a way for doing this.
In my Type I have:
private $securityContext;
public function __construct(SecurityContext $securityContext)
{
$this->securityContext = $securityContext;
}
....
public function buildForm(FormBuilderInterface $builder, array $options)
{
$disabled = false;
if(false === $this->securityContext->isGranted('ROLE_SUPER_ADMIN')) {
$disabled = true;
}
....
$builder->create('payments', 'form', array('virtual' => true, 'attr' => array('class' => 'form-section')))
->add('commission', 'integer')
->add('cashbackThreshold', 'integer', array(
'disabled' => $disabled
))
And than in my controller I have:
$form = $this->createForm(new WhiteLabelType($this->get('security.context')), $whiteLabel);