处理请求无法执行

时间:2016-07-09 00:01:34

标签: php symfony twig symfony-forms

目前我正在使用symfony 2.8,我遇到了一个问题  无法执行$form->handleRequest($request),并且表单中的数据无法填充数据库。当我die($request)时,我获得了我在视图中输入的所有数据。这是我的代码:

Enterprise.php(实体)它是从FOS用户继承的

<?php

namespace UtilisateurBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Enterprise
 *
 * @ORM\Table(name="enterprise")
 * @ORM\Entity(repositoryClass="UtilisateurBundle\Repository\EnterpriseRepository")
 */
class Enterprise extends Utilisateur
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="logo", type="string", length=255)
     */
    private $logo;

    /**
     * @var string
     *
     * @ORM\Column(name="raison_social", type="string", length=255)
     */
    private $raisonSocial;

    /**
     * @var string
     *
     * @ORM\Column(name="denomination", type="string", length=255)
     */
    private $denomination;

    /**
     * @var string
     *
     * @ORM\Column(name="slogan", type="string", length=255)
     */
    private $slogan;

    /**
     * @var string
     *
     * @ORM\Column(name="presentation", type="string", length=255)
     */
    private $presentation;

    /**
     * @var string
     *
     * @ORM\Column(name="code_postal", type="string", length=255)
     */
    private $codePostal;

    /**
     * @var string
     *
     * @ORM\Column(name="longitude", type="string", length=255)
     */
    private $longitude;

    /**
     * @var string
     *
     * @ORM\Column(name="laltitude", type="string", length=255)
     */
    private $laltitude;

    /**
     * @var string
     *
     * @ORM\Column(name="registre_commerce", type="string", length=255)
     */
    private $registreCommerce;

    /**
     * @var string
     *
     * @ORM\Column(name="date_creation", type="string", length=255)
     */
    private $dateCreation;

    /**
     * @var string
     *
     * @ORM\Column(name="effectif", type="string", length=255)
     */
    private $effectif;

    /**
     * @var string
     *
     * @ORM\Column(name="capital_social", type="string", length=255)
     */
    private $capitalSocial;

    /**
     * @var string
     *
     * @ORM\Column(name="fax", type="string", length=255)
     */
    private $fax;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set logo
     *
     * @param string $logo
     * @return Enterprise
     */
    public function setLogo($logo)
    {
        $this->logo = $logo;

        return $this;
    }

    /**
     * Get logo
     *
     * @return string 
     */
    public function getLogo()
    {
        return $this->logo;
    }

    /**
     * Set raisonSocial
     *
     * @param string $raisonSocial
     * @return Enterprise
     */
    public function setRaisonSocial($raisonSocial)
    {
        $this->raisonSocial = $raisonSocial;

        return $this;
    }

    /**
     * Get raisonSocial
     *
     * @return string 
     */
    public function getRaisonSocial()
    {
        return $this->raisonSocial;
    }

    /**
     * Set denomination
     *
     * @param string $denomination
     * @return Enterprise
     */
    public function setDenomination($denomination)
    {
        $this->denomination = $denomination;

        return $this;
    }

    /**
     * Get denomination
     *
     * @return string 
     */
    public function getDenomination()
    {
        return $this->denomination;
    }

    /**
     * Set slogan
     *
     * @param string $slogan
     * @return Enterprise
     */
    public function setSlogan($slogan)
    {
        $this->slogan = $slogan;

        return $this;
    }

    /**
     * Get slogan
     *
     * @return string 
     */
    public function getSlogan()
    {
        return $this->slogan;
    }

    /**
     * Set presentation
     *
     * @param string $presentation
     * @return Enterprise
     */
    public function setPresentation($presentation)
    {
        $this->presentation = $presentation;

        return $this;
    }

    /**
     * Get presentation
     *
     * @return string 
     */
    public function getPresentation()
    {
        return $this->presentation;
    }

    /**
     * Set codePostal
     *
     * @param string $codePostal
     * @return Enterprise
     */
    public function setCodePostal($codePostal)
    {
        $this->codePostal = $codePostal;

        return $this;
    }

    /**
     * Get codePostal
     *
     * @return string 
     */
    public function getCodePostal()
    {
        return $this->codePostal;
    }

    /**
     * Set longitude
     *
     * @param string $longitude
     * @return Enterprise
     */
    public function setLongitude($longitude)
    {
        $this->longitude = $longitude;

        return $this;
    }

    /**
     * Get longitude
     *
     * @return string 
     */
    public function getLongitude()
    {
        return $this->longitude;
    }

    /**
     * Set laltitude
     *
     * @param string $laltitude
     * @return Enterprise
     */
    public function setLaltitude($laltitude)
    {
        $this->laltitude = $laltitude;

        return $this;
    }

    /**
     * Get laltitude
     *
     * @return string 
     */
    public function getLaltitude()
    {
        return $this->laltitude;
    }

    /**
     * Set registreCommerce
     *
     * @param string $registreCommerce
     * @return Enterprise
     */
    public function setRegistreCommerce($registreCommerce)
    {
        $this->registreCommerce = $registreCommerce;

        return $this;
    }

    /**
     * Get registreCommerce
     *
     * @return string 
     */
    public function getRegistreCommerce()
    {
        return $this->registreCommerce;
    }

    /**
     * Set dateCreation
     *
     * @param string $dateCreation
     * @return Enterprise
     */
    public function setDateCreation($dateCreation)
    {
        $this->dateCreation = $dateCreation;

        return $this;
    }

    /**
     * Get dateCreation
     *
     * @return string 
     */
    public function getDateCreation()
    {
        return $this->dateCreation;
    }

    /**
     * Set effectif
     *
     * @param string $effectif
     * @return Enterprise
     */
    public function setEffectif($effectif)
    {
        $this->effectif = $effectif;

        return $this;
    }

    /**
     * Get effectif
     *
     * @return string 
     */
    public function getEffectif()
    {
        return $this->effectif;
    }

    /**
     * Set capitalSocial
     *
     * @param string $capitalSocial
     * @return Enterprise
     */
    public function setCapitalSocial($capitalSocial)
    {
        $this->capitalSocial = $capitalSocial;

        return $this;
    }

    /**
     * Get capitalSocial
     *
     * @return string 
     */
    public function getCapitalSocial()
    {
        return $this->capitalSocial;
    }

    /**
     * Set fax
     *
     * @param string $fax
     * @return Enterprise
     */
    public function setFax($fax)
    {
        $this->fax = $fax;

        return $this;
    }

    /**
     * Get fax
     *
     * @return string 
     */
    public function getFax()
    {
        return $this->fax;
    }
}

EnterpriseType.php

<?php

namespace UtilisateurBundle\Form;

use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class EnterpriseType extends BaseType {

    private $class;

    /**
     * @param string $class The User class name
     */
    public function __construct()
    {
        $this->class = 'UtilisateurBundle\Entity\Enterprise';
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {
        parent::buildForm($builder, $options);
        $builder
                ->add("nom", TextType::class, array('label' => 'Nom', 'max_length' => 255, "required" => true))
                ->add("logo", TextType::class, array('label' => 'logo', 'max_length' => 255, "required" => true))
                ->add("raisonSocial", TextType::class, array('label' => 'Eaison Social', 'max_length' => 255, "required" => true))
                ->add("denomination", TextType::class, array('label' => 'denomination', 'max_length' => 255, "required" => true))
                ->add("slogan", TextType::class, array('label' => 'sloganl', 'max_length' => 255, "required" => true))
                ->add("presentation", TextType::class, array('label' => 'presentation', 'max_length' => 255, "required" => true))
                ->add("codePostal", TextType::class, array('label' => 'code Postal', 'max_length' => 255, "required" => true))
                ->add("longitude", TextType::class, array('label' => 'longitude', 'max_length' => 255, "required" => true))
                ->add("laltitude", TextType::class, array('label' => 'laltitude', 'max_length' => 255, "required" => true))
                ->add("registreCommerce", TextType::class, array('label' => 'registre Commerce', 'max_length' => 255, "required" => true))
                ->add("dateCreation", TextType::class, array('label' => 'date Creation', 'max_length' => 255, "required" => true))
                ->add("effectif", TextType::class, array('label' => 'effectif', 'max_length' => 255, "required" => true))
                ->add("capitalSocial", TextType::class, array('label' => 'capital Social', 'max_length' => 255, "required" => true))
                ->add("fax", TextType::class, array('label' => 'fax', 'max_length' => 255, "required" => true))
        ;

    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {

        $resolver->setDefaults(array(
            'data_class' => $this->class,
        ));
    }

    // BC for SF < 3.0
    public function getName()
    {

        return $this->getBlockPrefix();
    }

    public function getBlockPrefix()
    {

        return 'meubles_tunis_user_registration';
    }

}

RegistrationEnterpriseController

<?php

namespace UtilisateurBundle\Controller;

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use FOS\UserBundle\Model\UserInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use UtilisateurBundle\Entity\Enterprise;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class RegistrationEnterpriseController extends Controller {

    /**
     * @Route("/register_enterprise",name="meubles_tunis_registration_register_enterprise")
     */
    public function registerAction(Request $request) {

        /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
        //$formFactory = $this->get('fos_user.registration_enterprise.form.factory');
        $form = $this->container->get('form.factory')->create(new \UtilisateurBundle\Form\EnterpriseType(), new \UtilisateurBundle\Entity\Enterprise());

        /** @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 = new Enterprise();

        $user->setEnabled(true);

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

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


        $form->setData($user);
        $form->handleRequest($request);

        if ($form->isValid()) {

            die($user->getFax());
            $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;
        }

        return $this->render('FOSUserBundle:Registration:register_enterprise.html.twig', array(
                    'form' => $form->createView(),
        ));
    }

}

FOS用户界面的registration.xml

<?xml version="1.0" encoding="UTF-8" ?>

<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="fos_user.registration_enterprise.form.factory" class="UtilisateurBundle\Form\EnterpriseType">
            <argument type="service" id="form.factory" />
            <argument>%fos_user.registration.form.name%</argument>
            <argument>%fos_user.registration.form.type%</argument>
            <argument>%fos_user.registration.form.validation_groups%</argument>
        </service>


        <service id="fos_user.registration_enterprise.form.type" class="UtilisateurBundle\Form\EnterpriseType">
            <tag name="form.type" alias="meubles_tunis_user_registration" />
            <argument>%fos_user.model.user.class%</argument>
        </service>

        <service id="fos_user.registration.form.type" class="FOS\UserBundle\Form\Type\RegistrationFormType">
            <tag name="form.type" alias="fos_user_registration" />
            <argument>%fos_user.model.user.class%</argument>
        </service>

    </services>

</container>

1 个答案:

答案 0 :(得分:0)

这是为了什么?

if (null !== $event->getResponse()) {
    return $event->getResponse();
}
如果您$form->handleRequest($request);

return $event->getResponse()将永远不会被调用。

return 终止当前函数中的执行,当然还会返回一些值。在这种情况下,它会返回$event->getResponse()

我认为您正在尝试发送活动。请查看Dispatcher docs中的此示例。