Symfony2: How can I send the current user to the edit form without overwritting the entity to edit?

Splendonia

Okay, so I need to access the current user's role in the form in order to query different things according to the role, it works just fine when creating a new user. Send the logged in user to the form as $options... however, I've noticed when editing the form, the $data option inside $options, it's the entity to edit...

How can I send the current user along the entity to edit without overwriting any of them?

I was trying this:

$current_user = $this->get('security.context')->getToken()->getUser();
        $form = $this->createForm(new UserType(), $entity, array(
            'action' => $this->generateUrl('user_update', array('id' => $entity->getId())),
            'method' => 'POST',
            'data' => $current_user,
        ));

but the 'data' option seems to overwrite the $entity I send above.

How can I send both?

Daniel Ribeiro

I think to achieve a higher level of quality you would need to declare your form type as a service and receive the SecurityContext object as a dependency, like this:

services:
    form.type.user:
        class: Acme\DemoBundle\Form\Type\UserType
        arguments: ["@security.context"]
        tags:
            - { name: form.type, alias: user_type } 

And declare the form type like this:

class UserType extends AbstractType {


    private $securityContext;

    public function __construct(SecurityContext $securityContext)
    {
        $this->securityContext = $securityContext;
    }
}

That way you can access the user through the SecurityContext object, which will be injected automatically by Symfony.

Create the form like this:

$form = $this->createForm($this->get('form.type.user'), $data);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Symfony2: How can I send the current user to the edit form without overwritting the entity to edit?

From Dev

Symfony2 Edit Without Form Builder

From Dev

How can I update my entity using POST method without form in Symfony2

From Dev

Symfony2 simple file upload edit without entity

From Dev

How define textarea content on form edit (Symfony2/TWIG)

From Dev

how do i get data of the selected row in new form and user can edit it?

From Dev

Symfony2: SonataAdminBundle - How can i get the object representing the current user inside an admin class?

From Dev

Symfony2: SonataAdminBundle - How can i get the object representing the current user inside an admin class?

From Dev

How can I drag jqgrid edit form properly?

From Dev

How can i allow a user to edit their own account details?

From Dev

How can I edit this code for user input and to end at 0?

From Dev

How can I edit a Deployment without modify the file manually?

From Dev

How can I edit an item in a list without changing the whole list?

From Dev

How can I validate/post col with edit type as select in jqgrid in form edit

From Dev

How can I edit a QListWidget item without removing it to edit and adding back?

From Dev

How I can add new field into "Customizable Options" of product's edit form in magento 2?

From Dev

How can I edit an xml line from user data in ec2 instance?

From Dev

I can not edit and submit form from jquery

From Dev

Can I edit hosts without sudo?

From Dev

How to update mysql using an edit button that would send the current data into forms from the simple CMS i made

From Dev

Edit groups in user edit form in Django

From Dev

Symfony2 - Edit label display name in form

From Dev

Symfony2: FOSUserBundle remove "Edit Username" form field in profile

From Dev

One to Many relations and add/edit form in SonataAdmin and Symfony2

From Dev

Symfony foreign keys (How can i edit them ?)

From Dev

Symfony2, FOSuser : Edit user with empty input password

From Dev

How do I edit more than one one-to-many entities in symfony2?

From Dev

Rails 4 : Current user can only edit their post

From Dev

Rails CanCan only current user can go through Edit

Related Related

  1. 1

    Symfony2: How can I send the current user to the edit form without overwritting the entity to edit?

  2. 2

    Symfony2 Edit Without Form Builder

  3. 3

    How can I update my entity using POST method without form in Symfony2

  4. 4

    Symfony2 simple file upload edit without entity

  5. 5

    How define textarea content on form edit (Symfony2/TWIG)

  6. 6

    how do i get data of the selected row in new form and user can edit it?

  7. 7

    Symfony2: SonataAdminBundle - How can i get the object representing the current user inside an admin class?

  8. 8

    Symfony2: SonataAdminBundle - How can i get the object representing the current user inside an admin class?

  9. 9

    How can I drag jqgrid edit form properly?

  10. 10

    How can i allow a user to edit their own account details?

  11. 11

    How can I edit this code for user input and to end at 0?

  12. 12

    How can I edit a Deployment without modify the file manually?

  13. 13

    How can I edit an item in a list without changing the whole list?

  14. 14

    How can I validate/post col with edit type as select in jqgrid in form edit

  15. 15

    How can I edit a QListWidget item without removing it to edit and adding back?

  16. 16

    How I can add new field into "Customizable Options" of product's edit form in magento 2?

  17. 17

    How can I edit an xml line from user data in ec2 instance?

  18. 18

    I can not edit and submit form from jquery

  19. 19

    Can I edit hosts without sudo?

  20. 20

    How to update mysql using an edit button that would send the current data into forms from the simple CMS i made

  21. 21

    Edit groups in user edit form in Django

  22. 22

    Symfony2 - Edit label display name in form

  23. 23

    Symfony2: FOSUserBundle remove "Edit Username" form field in profile

  24. 24

    One to Many relations and add/edit form in SonataAdmin and Symfony2

  25. 25

    Symfony foreign keys (How can i edit them ?)

  26. 26

    Symfony2, FOSuser : Edit user with empty input password

  27. 27

    How do I edit more than one one-to-many entities in symfony2?

  28. 28

    Rails 4 : Current user can only edit their post

  29. 29

    Rails CanCan only current user can go through Edit

HotTag

Archive