Car*_*ter 3 forms customization login symfony
如何创建使用表单构建器构建的自定义登录页面并使用表单辅助函数来呈现视图?我终于想通了!我的解决方案如下.
为什么需要?
好吧,我想使用表单生成器来创建Symfony登录页面,因为我想使用表单帮助程序函数来呈现视图.这样我的字段将始终使用正确的模板(手动编写表单 - 根据Symfony书籍示例 - 意味着如果主模板已更新,则登录表单的更改必须手动完成 - 并且有可能忘记!)
你可以做得更简单,更好(内置错误处理),解决方案就在Symfony :) ...看看这个类作为一个例子UserLoginType:
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <henrik@bjrnskov.dk>
* @author Jeremy Mikola <jmikola@gmail.com>
*/
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',
));
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
我使用了没有类的 createFormBuilder (请参阅Symfony - 使用没有类的表单),这样我就可以只渲染登录所需的字段:
/**
* @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(),
)
);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我以通常的方式渲染表单,除了使用full_name表单 twig 变量(请参阅Symfony - 表单变量参考_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 %}
Run Code Online (Sandbox Code Playgroud)
我不确定这仅限于哪个版本 - 我正在使用当前的 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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用createForm()以下方式获取表单:
$form = $this->createForm(\AppBundle\Form\LoginType::class, $defaultData);
Run Code Online (Sandbox Code Playgroud)