Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 4 get current user in form type?

I want to know how I can recover the current user in my FormType (EntityType)?

Currently, I can only retrieve the list of registered users but not the current (connected) user.

My current FormType

<?php

namespace App\Form;

use App\Entity\User;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class OneNewCarType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                'author',
                EntityType::class, [
                    'label' => 'Ville',
                    'class' => User::class,
                    'attr' => [
                        'class' => 'selectpicker'
                    ],
                    'choice_label' => function ($author) {
                        return $author->getCity();
                    }
                ]
            );
    }

    public function getBlockPrefix()
    {
        return 'oneNewCarType';
    }
}

I know how to recover it directly in my controller. But, I do not know how to do it when one uses the stepper bundle CraueFormFlowBundle

This is my current controller

/**
 * @Route("/classified/{id}/edit", name="classified_edit")
 * @param CarVehicle $carVehicle
 * @param ObjectManager $manager
 * @param CreateVehicleFlow $createVehicleFlow
 * @return RedirectResponse|Response
 */
public function edit(CarVehicle $carVehicle, ObjectManager $manager, CreateVehicleFlow $createVehicleFlow)
{
    $flow = $createVehicleFlow;
    $flow->bind($carVehicle);

    $form = $flow->createForm();
    if ($flow->isValid($form)) {
        $flow->saveCurrentStepData($form);
        if ($flow->nextStep()) {
            $form = $flow->createForm();
        } else {
            $manager->persist($carVehicle);
            $manager->flush();

            $flow->reset();

            $this->addFlash(
                'success',
                "Votre annonce <i>{$carVehicle->getId()}</i> a bien été mise à jour"
            );

            return $this->redirect($this->generateUrl('account_index'));
        }
    }

    return $this->render('front/classified/edit_vehicle.html.twig', [
        'form' => $form->createView(),
        'flow' => $flow,
        'carVehicle' => $carVehicle,
    ]);
}

Thanks for the help !

like image 700
nemoxi Avatar asked Sep 08 '19 17:09

nemoxi


2 Answers

With Symfony\Component\Security\Core\Security you can get user where you want. Inject this into FormType and use $user = $this->security->getUser();

private $security;

public function __construct(Security $security)
{
    $this->security = $security;
}
like image 136
Ihor Kostrov Avatar answered Oct 05 '22 12:10

Ihor Kostrov


In Symfony 5, With use

Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface

you can get user where you want. Inject this into FormType and use

$user = $this->token->getToken()->getUser();

private $token;

public function __construct(TokenStorageInterface $token)
{
   $this->token = $token;
}
like image 20
Berthol Yvano Avatar answered Oct 05 '22 11:10

Berthol Yvano