我正在使用FOS用户捆绑包+ Symfony2.3。 我想覆盖FOS用户包表单。覆盖并希望在头文件中添加登录和注册表单。对于show in header我使用的是javascript。例如,当我点击tab-2然后打开注册表单
有一件事我只是覆盖了这个,这是有效的,但当注册表格调用其他HTML时,那么它无法正常工作
我这样做但是这是我的错误:
The CSRF token is invalid. Please try to resubmit the form.
我不知道为什么?
我的档案是:
HeaderController.php
<?php
namespace XYZ\FrontBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use XYZ\UserBundle\Form\Type\RegistrationFormType;
class HeaderController extends Controller
{
/**
* @Route("/header", name="header")
* @Template()
*/
public function indexAction()
{
$registrationForm = $this->createForm(new RegistrationFormType());
return array('registration_form' => $registrationForm->createView());
}
}
?>
头(index.html.twig): -
{# Signup #}
<div id="tab-2" class="login_form">
<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(registration_form) }} method="POST" class="fos_user_registration_register">
<label><strong>Full Name</strong>
{{ form_widget(registration_form.username) }}
</label>
<label><strong>Email Address</strong>
{{ form_widget(registration_form.email) }}
</label>
<label><strong>Password</strong>
{{form_errors(registration_form.plainPassword)}}
{{ form_widget(registration_form.plainPassword.first, { 'attr':{'label': 'Password:'} }) }}
</label>
<label><strong>Confirm Password</strong>
{{form_errors(registration_form.plainPassword)}}
{{ form_widget(registration_form.plainPassword.second, { 'attr':{ 'label': 'Retype Password:'} }) }}</label>
<input type="submit" class="submitBut" value="{{ 'registration.submit'|trans({}, 'FOSUserBundle') }}"/>
</form>
</div>
RegistrationFormType.php
<?php
namespace XYZ\UserBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class RegistrationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
->add('plainPassword', 'repeated', array(
'type' => 'password',
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => "XYZ\UserBundle\Entity\User"
));
}
public function getName()
{
return 'xyz_user_registration';
}
}
的services.xml
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="xyz_user.registration.form.type" class="XYZ\UserBundle\Form\Type\RegistrationFormType">
<tag name="form.type" alias="xyz_user_registration" />
<argument>%fos_user.model.user.class%</argument>
</service>
</services>
</container>
RegisterController.php
<?php
namespace XYZ\UserBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Exception\AccountStatusException;
use FOS\UserBundle\Model\UserInterface;
/**
* Controller managing the registration
*
*/
class RegistrationController extends ContainerAware
{
public function registerAction()
{
$form = $this->container->get('fos_user.registration.form');
$formHandler = $this->container->get('fos_user.registration.form.handler');
$confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
$process = $formHandler->process($confirmationEnabled);
if ($process) {
$user = $form->getData();
$authUser = false;
if ($confirmationEnabled) {
$this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
$route = 'fos_user_registration_check_email';
} else {
$authUser = true;
$route = 'fos_user_registration_confirmed';
}
$this->setFlash('fos_user_success', 'registration.flash.user_created');
$url = $this->container->get('router')->generate($route);
$response = new RedirectResponse($url);
if ($authUser) {
$this->authenticateUser($user, $response);
}
return $response;
}
return $this->container->get('templating')->renderResponse('XYZUserBundle:Registration:register.html.'.$this->getEngine(), array(
'form' => $form->createView(),
));
}
}
?>
config.yml
fos_user:
db_driver: orm
firewall_name: main
user_class: XYZ\UserBundle\Entity\User
registration:
form:
type: xyz_user_registration
name: xyz_user_registration_form
validation_groups: [Registration, Default]
谢谢!
答案 0 :(得分:6)
表单提交使用隐藏输入中包含的CSRF令牌。你不要在视图中插入此输入。 尝试在提交按钮之前插入此行:
{{ form_rest(registration_form) }}
答案 1 :(得分:2)
您的FormType不正确。查看https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_forms.md
<?php
namespace XYZ\UserBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
class RegistrationFormType extends BaseType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => "XYZ\UserBundle\Entity\User"
));
}
public function getName()
{
return 'xyz_user_registration';
}
}
答案 2 :(得分:0)
{{ form_end(form) }}
为我工作。 添加了所有遗漏的字段,您可能忘了添加。隐藏的令牌输入字段* 例如。
<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register">
{{form_label(form.email)}}: {{form_widget(form.email)}}
{# ... #}
{{ form_end(form) }}
</form>
*
<input type="hidden" id="fos_user_registration_form__token" name="fos_user_registration_form[_token]" value="fsdfsdfsdf" />
希望它有所帮助。 : - )