2017-07-14 116 views
0

我使用symfony的3.3Symfony的FormBuilder登錄

I'have創建FormBuilder用於登錄

,這是我的路由控制器:

/** 
* @Route("/login", name="app.login") 
*/ 
public function loginAction() 
{ 
    $form = $this->createForm(LoginType::class); 
    return $this->render('@site_theme/UserLogin.html.twig', ['form' => $form->createView()]); 
} 

和我的嫩枝:

{{ form_start(form, {'action': path('sec_check'), 'method': 'POST'}) }} 
    {{ form_errors(form) }} 
{{ form_row(form.username) }} 
    {{ form_row(form.plainPassword) }} 
<input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}"/> 
    {{ form_row(form.submit) }} 
{{ form_end(form) }} 

我的FormBuilder登錄類型:

<?php 

namespace AppBundle\Forms; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\Form\FormError; 
use Symfony\Component\Form\FormEvents; 
use Symfony\Component\Form\FormEvent; 
use Symfony\Component\HttpFoundation\RequestStack; 
use Symfony\Component\OptionsResolver\OptionsResolver; 
use Symfony\Component\Security\Core\Security; 

/* TYPES */ 
use Symfony\Component\Form\Extension\Core\Type\EmailType; 
use Symfony\Component\Form\Extension\Core\Type\PasswordType; 
use Symfony\Component\Form\Extension\Core\Type\SubmitType; 


class LoginType extends AbstractType 
{ 
    private $requestStack; 

    public function __construct(RequestStack $requestStack) 
    { 
     $this->requestStack = $requestStack; 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('username', EmailType::class, [ 
       'attr' => ['required' => true, 'placeholder' => 'email'], 
       'label' => false 
      ]) 
      ->add('plainPassword', PasswordType::class, [ 
       'attr' => ['required' => true, 'placeholder' => 'password'], 
       'label' => false 
      ]) 
      ->add('submit', SubmitType::class, [ 
       'label' => 'enter', 
       'attr' => ['class' => 'btn-login'], 
      ]); 
     $request = $this->requestStack->getCurrentRequest(); 
     $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($request) { 
      if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) { 
       $error = $request->attributes->get(Security::AUTHENTICATION_ERROR); 
      } else { 
       $error = $request->getSession()->get(Security::AUTHENTICATION_ERROR); 
      } 
      if ($error) { 
       $event->getForm()->addError(new FormError($error->getMessage())); 
      } 
     }); 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults([ 
      'csrf_token_id' => 'authenticate', 
      'csrf_field_name' => 'login', 
      'intention' => 'authenticate', 
      'attr' => ['role' => 'form', 'class' => 'navbar-form'], 
      'method' => 'POST' 
     ]); 
    } 
} 

當我嘗試登錄我越來越:

認證錯誤。

首先,我怎麼能打印所有數據[用戶名,密碼]

這是送?

我該如何解決這個問題?

非常感謝

回答

0

我固定它由remove login_username到_username

$form = $this->get('form.factory')->createNamed(null, LoginType::class);