0

I'm developing an app in symfony for follow the workflow of a document. In the lifecycle of the document, take part various departments, I mean, people of each department with his own role inside de app (ROLE_USER, ROLE_SYSTEMS...). I have the same form for all of them but in the different states of the lifecycle of the document one of them has to complete specific fields and the rest will be disable or readonly.

I want to know how is the best way to do this in Symfony, check the role and disable the fileds the user can't edit in every moment of the lifecycle.

I have investigated and find something like this for my twig templates, but I will have to do this for each field and each role and I don't know if I can apply this to field attributes like disabled or readonly, is it?.

{{% if is_granted('ROLE_ADMIN') %}
    <a href="...">Delete</a>
{% endif %}

I also heard about voters but I don't have very clear how it works and if it is appropriate for my situation.

What do you think?

  • I learned more about voters and i think it could be the solution, if only i know how to compare role of the user and state of the lifecycle of the document to know what fields can edit the user logged. – JEnriquePM May 17 '17 at 15:15
  • Well, i coded a voter that check the document object state and the role of the user logged, now my question is, the voter has to be asociated to the class document? I can call the voter anywhere i want with the attribute defined on it, 'edit' in my case but i dont know if im doing something wrong or inapropiated. Is my first voter, be kind with me. XD. Should i post the code? – JEnriquePM May 18 '17 at 10:49
  • I forgot to post before, i solved my problem with the voter to grant access to the role i want to do it, at the end i do a double ckeck with the voter and the owner of the document to grant access to every field i want individually. If someone are interested in the solution or the voter i can post the code, no time right now. – JEnriquePM Jun 09 '17 at 12:21

1 Answers1

0

Easy but ugly way would be different form type classes (just to play around to find out which fields are necessary and which aren't, but it's optional).

The better way would be FormEvents like PRE_SET_DATA where you could add/remove (not sure if modifien also available).

Look here to find out more -> How to Dynamically Modify Forms Using Form Events

Basically you should try following:

public function buildForm(FormBuilderInterface $builder, array $options)
{ 
    // Add your general fields which are available for all
    //$builder->add(...)

    // at the end add an EventListener
    $builder->addEventListener(FormEvents::PRE_SET_DATA, [$this, 'onPreSetData']);
}

and in the same form-class create the onPreSetData method:

public function onPreSetData(FormEvent $formEvent)
{
    $form = $formEvent->getForm();
    $entity = $formEvent->getData();

    //if user is admin then $form->add(...) like above in with $buildForm->add(...)
}
V-Light
  • 3,065
  • 3
  • 25
  • 31