为什么 $form->isValid() 总是 return false?

Why $form->isValid() always return false?

我正在处理网站的登录页面。 当我点击登录按钮时没有任何反应,但是当我从代码中删除 $form->isValid() 时它工作正常。 经过几次测试,我发现 $form->isValid() 总是 returns 错误! 我不明白为什么总是 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(),
    ]);
}

查看页面的代码如下:

<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>


    <div class="inset">
        <div class="wrap-input100 " data-validate="Username is required">

            {{ form_widget(form.username, {'attr': {'class': 'input100', 'placeholder': 'Username', 'name':'username', 'type': 'text'} }) }}
        </div>


        <div class="wrap-input100 validate-input m-b-10" data-validate="Password is required">

            {{ form_widget(form.password, {'attr': {'class': 'input100', 'placeholder': 'Password', 'name':'password', 'type': 'password'}})}}

        </div>

    </div>
    <p class="p-container">
        <input type="submit" name="Login" id="go" value="Login">
    </p>
</form>

出现这种情况是因为表单中没有csrf token,所以在结束表单之前加上这个

{{ form_rest(form) }}