我创建了这样的登录表单:https://symfony.com/doc/current/security/form_login_setup.html
但是我想在这个控制器中创建另一种形式:重置密码和注册
但提交只能在登录时使用,但从重置表单提交不提交
控制器:
/**
* @Route("/login",name="login")
*/
public function loginAction(Request $request)
{
$Session = $this->get('session');
$authenticationUtils = $this->get('security.authentication_utils');
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
$rememberPassword = $this->createForm(RememberPasswordType::class, array(
'action' => $this->generateUrl('login'),
));
$rememberPassword->handleRequest($request);
if($request->isMethod('POST')){
if ($rememberPassword->isSubmitted() && $rememberPassword->isValid()) {
try {
$userEmail = $rememberPassword->get('email')->getData();
$userManager = $this->get('user_manager');
$userManager->sendResetPasswordLink($userEmail);
$Session->getFlashBag()->add('success', 'Zostało wysłane!');
return $this->redirect($this->generateUrl('login'));
} catch (UserException $exc) {
$error = new FormError($exc->getMessage());
$rememberPassword->get('email')->addError($error);
}
}
}
return $this->render('UserBundle:Login:login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
'rememberPassword' => $rememberPassword->createView()
));
}
表格类型:
class RememberPasswordType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('email', EmailType::class, array(
'label' => 'Email',
'constraints' => array(
new Assert\NotBlank(),
new Assert\Email()
)
))
->add('save', SubmitType::class, array(
'label' => 'Przypomnij hasło'
));
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => null,
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix() {
return 'rememberPassword';
}
}
答案 0 :(得分:0)
您需要单独处理表单。
if ($request->isMethod('POST')) {
$form1->handleRequest($request);
$form2->handleRequest($request);
if ($form1->isSubmitted()) {
// Handle $form1
} else if ($form2->isSubmitted()) {
// Handle $form2
}
}