I made registration with FosUserBundle Symfony 4.1 I created a separate controller and view, the registration works and is available at site / register link

BUT! I need to make the registration be in a modal window, and work from every page. Displays the form through

{{ render(controller('App\\Controller\\RegistrationController::register')) }} 

the form is displayed in base.html.twig, but when the form is submitted the user is not added to the database, the RegistrationController does not work. Please help with this problem.

    1 answer 1

    The issue is resolved. Added an action parameter when creating a form in the controller

     $form = $this->createForm(UserType::class, $user, ['action' => $this->generateUrl('user_registration')]); 

    The result was:

    RegistrationController:

     namespace App\Controller; use App\Entity\User; use App\Form\UserType; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; class RegistrationController extends Controller { /** * @Route("/register", name="user_registration") */ public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder) { // 1) build the form $user = new User(); $form = $this->createForm(UserType::class, $user, ['action' => $this->generateUrl('user_registration')]); // 2) handle the submit (will only happen on POST) $form->handleRequest($request); if($form->isSubmitted() && $form->isValid()) { // 3) Encode the password (you could also do this via Doctrine listener) $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword()); $user->setPassword($password); // 4) save the User! $entityManager = $this->getDoctrine()->getManager(); $entityManager->persist($user); $entityManager->flush(); // ... do any other work - like sending them an email, etc // maybe set a "flash" success message for the user return $this->redirectToRoute('mainPage'); } return $this->render( 'registration/register.html.twig', array('form' => $form->createView()) ); } } 

    register.html.twig:

     {{ form_start(form) }} <div class="modal-body"> {{ form_row(form.username) }} {{ form_row(form.email) }} {{ form_row(form.plainPassword.first) }} {{ form_row(form.plainPassword.second) }} </div> <div class="modal-footer"> <button type="submit" class="button button-bordered m-auto">Зарегистрироваться</button> </div> {{ form_end(form) }} 

    And we use simply through the render of the controller on any page:

     {{ render(controller('App\\Controller\\RegistrationController::register')) }}