PHP-SYMFONY 3(为什么我的表单不验证请求数据?)

时间:2017-05-12 16:16:46

标签: symfony request

一切都在标题中,我试图提交&验证Symfony表格,但我不能。 当我跟踪请求时,我发现表单没有在POST参数中发送任何数据(无请求内容,没有POST参数)! 我的代码是:

控制器中的

 use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
 use Symfony\Component\Form\Extension\Core\Type\EmailType;
 use Symfony\Component\Form\Extension\Core\Type\FormType;
 use Symfony\Component\Form\Extension\Core\Type\PasswordType;
 use Symfony\Component\Form\Extension\Core\Type\ResetType;
 use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Test\FormBuilderInterface;
use Symfony\Component\Form\Tests\Extension\Core\Type\CheckboxTypeTest;
use Symfony\Component\Form\Tests\Extension\Core\Type\RepeatedTypeTest;
use Symfony\Component\HttpFoundation\Request;
use gestionBundle\Models\rememberMe;
use Symfony\Component\HttpFoundation\Session\Session;

class DefaultController extends Controller
 {
  /**
   * @Route("/login",name="login")
   */
  public function loginAction(Request $request)
  {
    $p=new compte();
    $form=$this->createFormBuilder($p)

        ->add('mail',EmailType::class)
        ->add('password',PasswordType::class)
        ->add('login', SubmitType::class, array('label' => 'Login'))
        ->getForm();
    $form->handleRequest($request);


    $em=$this->getDoctrine()->getManager();
    $repository=$em->getRepository('gestionBundle:compte');


    $session=$this->container->get('session');
    print_r($_REQUEST);
    if ($request->getMethod()=='POST'){


        $data = $request->request->all();
        $email = $data['form']['mail'];
        $password = $data['form']['password'];


$user=$repository>findOneBy(array('mail'=>$email,'password'=>$password));

       if($user)
       {


           $remember=$request->request->get('remember_me');

           if($remember=='remember me'){

                $rememberMe=new rememberMe();
               $rememberMe->setEmail($email);
               $rememberMe->setPassword($password);

               $session = new Session();
               $session->start();
               $session->set('rememberMe',$rememberMe);
           }

  return 
 $this>render('gestionBundle:Default:index.html.twig',array('user'=>$user));
       }

       else{
           $this->addFlash('alerte','LOGIN/PASSWORD INCORRECTE.. RESSAYER !');


       }

   }
    else{

        if ($session->has('rememberMe')){
            echo 'cas contarire 2';
            $rememberMe=$session->get('rememberMe');
            $email=$rememberMe->getemail();
            $password=$rememberMe->getpassword();
            $user=$repository->findOneBy(array('mail'=>$email,'password'=>$password));

            if($user)
            {

                return $this-
     >render('gestionBundle:Default:index.html.twig',array('user'=>$user));
            }
        }
    }
    return $this->render('gestionBundle:Default:login.html.twig',array('fr'=>$form->createView()));

}
}

我的Twig文件:

我的TWIG文件都没问题,因为我已经成功完成了函数' loginAction'没有任何问题! 我不知道< loginAction'中的错误是什么?功能?

谢谢你们的帮助。

1 个答案:

答案 0 :(得分:0)

改变这个:

public function device()
{
return  $this->hasMany('App\Device');
}

为此:

if ($request->getMethod()=='POST'){