0

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);
Community
  • 1
  • 1
Tommuu
  • 81
  • 1
  • 11
  • "but I can't use an if-statement in the form-builder" Hmm, but what's the constraint that prevent you from using an if statement in your form builder? Check the approach [here](http://stackoverflow.com/questions/19813503/symfony-fos-pass-the-user-id-variable-to-a-form-type/19815866#19815866) to undertand the different ways to build a conditional form. The point here (in the example) is to get the user id, You've to consider using the same approach except that you need the user role. – Ahmed Siouani Oct 28 '14 at 11:23
  • @AhmedSiouani well, I wasn't able to put an if-statement there as in this form-builder is an hierarchy. I am not working alone on this project so. But I've solved it for what I needed, check my edit for what I've come up with! Thanks anyway for the article you've linked! I am sure it will come in handy some time ;) – Tommuu Oct 28 '14 at 14:45

1 Answers1

0

Of course you can use an If conditional in the FormType :

if($this->user.hasRole(ROLE_SUPERADMIN)) {
  $builder->add('createdAt') 
}

But you need to inject the $user in the FormType Controller, or just this boolean you want to check, as an example:

private $user;
public function __construct($user) {
   $this->user = $user;
}

And also in the Controller when you instance the FormType, don't forget to add it :

$user = $this->get('security.context')->getToken()->getUser();
$form = $this->createForm ( new yourFormType($user) // ....  )

This is for sure not the recommended way to do it. I just wanted to help you to achieve what you wanted to do.

Martin Fasani
  • 825
  • 7
  • 20