2016-02-26 62 views
0

如何創建使用表單構建器和窗體幫助器函數來構建視圖的自定義登錄頁面?我終於想通了!我的解決方案如下。Symfony登錄頁面的表單生成器

爲什麼需要?

那麼我想使用表單生成器來創建Symfony登錄頁面,因爲我想使用窗體幫助函數來呈現視圖。這樣,我的字段將始終使用正確的模板(手動編寫表單 - 根據Symfony書籍示例 - 意味着如果主模板已更新,登錄表單的更改將不得不手動完成 - 並且有可能忘記!)

回答

1

我用createFormBuilder無類(見Symfony - Using a Form without a Class),所以我可能使剛剛進行登錄所需的字段:

/** 
* @Route("/login", name="login") 
*/ 
public function loginAction(Request $request) 
{ 
    if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) { 
     $this->addFlash('warning', 'You are already fully logged in.'); 
     return $this->redirectToRoute('my_homepage'); 
    } else { 
     $authenticationUtils = $this->get('security.authentication_utils'); 
     $defaultData = array('username' => $authenticationUtils->getLastUsername()); 
     $form = $this->createFormBuilder($defaultData) 
      ->add('username', \Symfony\Component\Form\Extension\Core\Type\TextType::class) 
      ->add('password', \Symfony\Component\Form\Extension\Core\Type\PasswordType::class) 
      ->add('logIn', \Symfony\Component\Form\Extension\Core\Type\SubmitType::class) 
      ->getForm(); 
     if (!is_null($authenticationUtils->getLastAuthenticationError(false))) { 
      $form->addError(new \Symfony\Component\Form\FormError(
       $authenticationUtils->getLastAuthenticationError()->getMessageKey() 
      )); 
     } 
     $form->handleRequest($request); 
     return $this->render('login.html.twig', array(
       'form' => $form->createView(), 
       ) 
     ); 
    } 
} 

然後我呈現的形式,以通常的方式,但我用了full_name形成小枝變量(請參閱Symfony - Form Variables Reference)以設置正確的字段名稱_username_password

{% extends 'base.html.twig' %} 

{% block title %}Log in to {{ app.request.host }}{% endblock %} 

{% block body %} 
    <h1>Account Log In</h1> 

    {{ form_start(form) }} 
    {{ form_row(form.username, {'full_name': '_username'}) }} 
    {{ form_row(form.password, {'full_name': '_password'}) }} 
    {{ form_errors(form) }} 
    {{ form_end(form) }} 
{% endblock %} 

我不確定這是什麼版本限制爲 - 我使用的是當前的3.0。

編輯

在你想知道的情況下..是的,你可以使用一個FormBuilderInterface創建如果你想在其他地方使用相同的形式(用不同的模板)的形式。這是相當標準的。您只需建立LoginType文件(或任何你選擇稱呼它):

namespace AppBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\Form\Extension\Core\Type\TextType; 
use Symfony\Component\Form\Extension\Core\Type\PasswordType; 
use Symfony\Component\Form\Extension\Core\Type\SubmitType; 

class LoginType extends AbstractType 
{ 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
       ->add('username', TextType::class) 
       ->add('password', PasswordType::class) 
       ->add('logIn', SubmitType::class); 
    } 

} 

,然後使用createForm(),而不是讓您的形式:

$form = $this->createForm(\AppBundle\Form\LoginType::class, $defaultData); 
3

你可以做到這一點更簡單,更好的(內置在錯誤處理)和解決方案是正確的,Symfony的:) ......看看這個類爲例UserLoginType

<?php 

/* 
* This file is part of the Symfony package. 
* 
* (c) Fabien Potencier <[email protected]> 
* 
* For the full copyright and license information, please view the LICENSE 
* file that was distributed with this source code. 
*/ 

namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\CsrfFormLoginBundle\Form; 

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; 

/** 
* Form type for use with the Security component's form-based authentication 
* listener. 
* 
* @author Henrik Bjornskov <[email protected]> 
* @author Jeremy Mikola <[email protected]> 
*/ 
class UserLoginType extends AbstractType 
{ 
    private $requestStack; 

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

    /** 
    * {@inheritdoc} 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('username', 'Symfony\Component\Form\Extension\Core\Type\TextType') 
      ->add('password', 'Symfony\Component\Form\Extension\Core\Type\PasswordType') 
      ->add('_target_path', 'Symfony\Component\Form\Extension\Core\Type\HiddenType') 
     ; 

     $request = $this->requestStack->getCurrentRequest(); 

     /* Note: since the Security component's form login listener intercepts 
     * the POST request, this form will never really be bound to the 
     * request; however, we can match the expected behavior by checking the 
     * session for an authentication error and last username. 
     */ 
     $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())); 
      } 

      $event->setData(array_replace((array) $event->getData(), array(
       'username' => $request->getSession()->get(Security::LAST_USERNAME), 
      ))); 
     }); 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function configureOptions(OptionsResolver $resolver) 
    { 
     /* Note: the form's csrf_token_id must correspond to that for the form login 
     * listener in order for the CSRF token to validate successfully. 
     */ 

     $resolver->setDefaults(array(
      'csrf_token_id' => 'authenticate', 
     )); 
    } 
} 
+0

我已經更新了它一下了Symfony4:https://開頭GIS t.github.com/fieg/33200ecd7aab92641bc4d2cebe2e9037 –