我提交表单

时间:2016-01-21 13:46:40

标签: php html forms symfony

我正试图从FOSUSERBUNDLE制作覆盖注册表;

我需要获取变量POST,但是当我提交表单点击提交按钮时,我有一个GET而不是POST方法!我不明白为什么?

RegistrationFormType.php

<?php


namespace BISSAP\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class RegistrationFormType extends AbstractType
{

    private $class;

    /**
     * @param string $class The User class name
     */
    public function __construct($class)
    {
        $this->class = $class;

    }


    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('email', 'email', array('required' => false,'validation_groups' => array('registration'), 
                        'label' => 'form.email',
                        'translation_domain' => 'FOSUserBundle',
                        'attr' => array('class' => 'input__registerbox')))
            ->setMethod('POST')
            ->add('username', null, array('required' => false, 'validation_groups' => array('registration'),
                        'label' => 'form.username',
                        'translation_domain' => 'FOSUserBundle',
                        'attr' => array('class' => 'input__registerbox')))
            ->add('plainPassword', 'repeated', array(
        'required' => false,
        'validation_groups' => array('registration'),
                'type' => 'password',
                'options' => array('translation_domain' => 'FOSUserBundle'),
                'first_options' => array('label' => 'form.password', 'attr' => array('class' => 'input__registerbox')),
                'second_options' => array('label' => 'form.password_confirmation', 'attr' => array('class' => 'input__registerbox')),
                'invalid_message' => 'fos_user.password.mismatch',
            ))

            //can't use this
            //->setAction($this->generateUrl('fos_user_registration_register', array('type' => 'agent'),true));

        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => $this->class,
            'intention'  => 'registration',
        ));
    }

    // BC for SF < 2.7
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $this->configureOptions($resolver);
    }

    public function getName()
    {
        return 'bissap_user_registration';
    }
}

RegistrationType.php

<?php
// src/UserBundle/Form/RegistrationType.php

namespace BISSAP\UserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class RegistrationType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name','text',array('required' => false,'validation_groups' => array('Registration'), 
                        'attr' => array('class' => 'input__registerbox')));
        $builder->add('firstname','text',array('required' => false,'validation_groups' => array('Registration'), 
                        'attr' => array('class' => 'input__registerbox')));
        $builder->add('Enregistrer', 'submit', array(
                'attr' => array(
                'class' => 'registerbox__submit bc-btn')));
        //$builder->setAction($this->router->generate('fos_user_registration_register', array('type' => 'agent')));

    }
/*
    public function getDefaultOptions()
    {
        return array(
            'validation_groups' => array('registration', 'Default')
        );
    }
*/ 
    public function configureOptions(OptionsResolver $resolver) 
    { 
        $resolver->setDefaults(array( 'data_class' => $this->class, 'intention' => 'Registration', 'validation_groups' => array('Registration'))); 
    }

    public function getParent()
    {
        return 'bissap_user_registration';
    }

    public function getName()
    {
        return 'app_user_registration';
    }
}

RegistrationController.php的一部分

class RegistrationController extends Controller
{
    public function registerAction(Request $request)
    {

>>>>>>>>>>>> $isPOST = $request->getMethod();<<<<<<<<<<<<


        /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
        $formFactory = $this->get('fos_user.registration.form.factory');
        /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
        $userManager = $this->get('fos_user.user_manager');
        /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
        $dispatcher = $this->get('event_dispatcher');

        $user = $userManager->createUser();
        $user->setEnabled(true);

        $event = new GetResponseUserEvent($user, $request);
        $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);

        if (null !== $event->getResponse()) {
            return $event->getResponse();
        }
        $entity = new User();

        $form = $this->createForm('app_user_registration', 
                                    $entity, 
                                    array('action' => $this->generateUrl('fos_user_registration_register', 
                                            array('type' => 'agent')),
                                            'validation_groups' => 'Registration'));



        $form->handleRequest($request);


        $url = $this->getRequest()->getUri();


        if ($form->isValid()) {
            $event = new FormEvent($form, $request);
            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);

            $userManager->updateUser($user);

            if (null === $response = $event->getResponse()) {
                $url = $this->generateUrl('fos_user_registration_confirmed');
                $response = new RedirectResponse($url);
            }

            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

            return $response;
        }
        else if ($request->getMethod() == 'POST') { return $this->redirect($url); }


         return $this->render('FOSUserBundle:Registration:register.html.twig', array(
            'form' => $form->createView(), 'error_form' => $form->getErrorsAsString(), 'isPOST' => $isPOST
        ));

    }

HTML代码(注册表单) [localhost / bodykoncept / web / app_dev.php /题词/代理]

<form name="app_user_registration" method="post" action="/bodykoncept/web/app_dev.php/inscription/agent">
<div id="app_user_registration">
<div>                
<label for="app_user_registration_email">Adresse e-mail</label>
<input id="app_user_registration_email" name="app_user_registration[email]" class="input__registerbox" type="email">
</div>
<div>
<label for="app_user_registration_username">Nom d'utilisateur</label>
<input id="app_user_registration_username" name="app_user_registration[username]" maxlength="255" pattern=".{2,}" class="input__registerbox" type="text">
</div>
<div>
<label for="app_user_registration_plainPassword_first">Mot de passe</label><input id="app_user_registration_plainPassword_first" name="app_user_registration[plainPassword][first]" class="input__registerbox" type="password">
</div>
<div>
<label for="app_user_registration_plainPassword_second">Vérification</label><input id="app_user_registration_plainPassword_second" name="app_user_registration[plainPassword][second]" class="input__registerbox" type="password">
</div>
<div>                
<label for="app_user_registration_name">Name</label><input id="app_user_registration_name" name="app_user_registration[name]" class="input__registerbox" type="text">
</div>
<div>                
<label for="app_user_registration_firstname">Firstname</label>
<input id="app_user_registration_firstname" name="app_user_registration[firstname]" class="input__registerbox" type="text">
</div>
<div>
<button type="submit" id="app_user_registration_Enregistrer" name="app_user_registration[Enregistrer]" class="registerbox__submit bc-btn">Enregistrer</button>
</div>
<input id="app_user_registration__token" name="app_user_registration[_token]" value="40Kv134YkDsTGixB6q-MovQ_VBAUDZvfuN6oCwnV-Ko" type="hidden">
</div>
</form>

我显示发送方法$request->getMethod();,但我有GET而没有POST。

0 个答案:

没有答案