我正在處理網(wǎng)站的登錄頁(yè)面。當(dāng)我單擊登錄按鈕時(shí)沒有任何反應(yīng),但是當(dāng)我 $form->isValid() 從代碼中刪除時(shí)它工作正常。經(jīng)過幾次測(cè)試,我發(fā)現(xiàn) $form->isValid() 總是返回 false!我不明白為什么總是這樣false!這是控制器的代碼:/** * @Route("/login", name="login", methods={"GET","POST"}) */public function login(Request $request): Response{ $user = new Admins(); $form = $this->createFormBuilder($user) ->add('username', TextType::class) ->add('password', PasswordType::class) ->getForm(); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $session =$request->getSession(); $session->clear(); $username =$user->getUsername(); $password =$user->getPassword(); $repository = $this->getDoctrine()->getRepository(Admins::class); $user1=$repository->findOneBy(array('username'=>$username,'password'=>$password)); if (!$user1) { $this->get('session')->getFlashBag()->add('info','The username or password is incorrect');} else { if (!$session->has('name')) { $session->set('username',$user1->getUsername()); $session->set('rank',$user1->getRank()); return $this->redirectToRoute('homepage'); } } } return $this->render('login/index.html.twig', [ 'user' => $user, 'form' => $form->createView(), ]);}這是查看頁(yè)面的代碼:<div class="text-center w-full p-t-25 p-b-50"> {% if app.session.flashBag.has('info') %} <div class="btn btn-warning"> {% for msg in app.session.flashBag.get('info') %} {{msg}} {% endfor %} </div> {% endif %}</div><form id="accesspanel" action=" {{ path ('login') }} " method="post"> <h1 id="litheader">Login Panel </h1>
1 回答

胡說(shuō)叔叔
TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
這是因?yàn)楸韱沃袥]有 csrf 令牌,所以在結(jié)束表單之前添加這個(gè)
{{ form_rest(form) }}
- 1 回答
- 0 關(guān)注
- 131 瀏覽
添加回答
舉報(bào)
0/150
提交
取消