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')) }}