Symfony 3.20
当您构建 FormType 时,在控制器中执行如下所示的路由和操作,使用“ if ($formRegister->isSubmitted() && $formRegister->getClickedButton('form2') &&...)”
class WelcomeController extends Controller
{
/**
* @Route("/welcome", name="welcome")
*/
public function welcomeAction(Request $request)
{
$uLogin = new User();
$formLogin = $this->createForm(LoginUserFormType::class, $uLogin);
$uRegister = new User();
$formRegister = $this->createForm(UserRegistrationFormType::class, $uRegister);
$authenticationUtils = $this->get('security.authentication_utils');
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
if ($request->isMethod('post')){
$formLogin->handleRequest($request);
$formRegister->handleRequest($request);
if($formLogin->isSubmitted() && $formLogin->getClickedButton('form1')){
return $this->redirectToRoute('login_success');
}
if ($formRegister->isSubmitted() && $formRegister->getClickedButton('form2') && $formRegister->isValid() ) {
$password = $this->get('security.password_encoder')
->encodePassword($uRegister, $uRegister ->getPlainPassword());
$uRegister ->setPassword($password);
$uRegister->setRole('ROLE_USER');
$em = $this ->getDoctrine() ->getManager();
$em -> persist($uRegister);
$em -> flush();
return $this->redirectToRoute('register_success');
}
}
return $this->render(
'form/welcome.html.twig',
array(
'form1' => $formLogin -> createView(),
'form2' => $formRegister -> createView(),
'last_username' => $lastUsername,
'error' => $error,
)
);
}
建立你的路由,就是这样:-)