Symfony2:表单提交执行2次 - 结果加倍

时间:2015-11-09 17:44:01

标签: php forms symfony form-submit

当我提交表格时,我的结果翻了一倍。找不到问题的原因。我只提交了一次表格,我只需要一个记录,但不知怎的,它只加倍了。

雇用是我的控制员:

\的appbundle \控制器\ DefaultController

 /**
 * @Route("/test/add", name="addTest", defaults={"_locale": "sr"}, requirements={
 *    "_locale": "en|sr"
 * })
 * @Method({"GET", "POST"})
 */
public function addTestAction(Request $request)
{
    $test = new Test();
    $testForm = $this->createForm(new TestType(), $test);

    if ($request->isMethod('POST'))
    {
        $testForm->submit($request->request->get($testForm->getName()));

        if($testForm->isValid())
        {
            $testFormData = $testForm->getData();
            exit(\Doctrine\Common\Util\Debug::dump($testFormData));
        }
    }

    return $this->render(
        'AppBundle:trt:addTest.html.twig', array(
            'form' => $testForm->createView(),
        )
    );
}

雇用是我的实体:

\的appbundle \实体\测试

class Test
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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


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

    /**
     * Set name
     *
     * @param string $name
     * @return Test
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

    /**
     * Set region
     *
     * @param string $region
     * @return Test
     */
    public function setRegion($region)
    {
        $this->region = $region;

        return $this;
    }

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

雇用是我的表格类型: \的appbundle \表格\类型

class TestType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'text')
            ->add('region', 'text')
        ;
    }

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

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Test',
        ));
    }
}

并查看:AppBundle \ Resources \ views \ trt \ addTest.html.twig

{% extends 'default/layout.html.twig' %}

{% block content %}

    <div><label>add Test view</label></div>

    {{ form_start(form) }}
    {{ form_row(form.name) }}
    {{ form_row(form.region) }}
    <div>
        <input type="submit" value="Dodaj test grad" />
    </div>
    {{ form_end(form) }}
{% endblock %}

提交表格结果后:

object(stdClass)[438]
  public '__CLASS__' => string 'AppBundle\Entity\Test' (length=21)
  public 'id' => null
  public 'name' => string 'Moscow' (length=6)
  public 'region' => string 'Moscow' (length=6)

object(stdClass)[438]
  public '__CLASS__' => string 'AppBundle\Entity\Test' (length=21)
  public 'id' => null
  public 'name' => string 'Moscow' (length=6)
  public 'region' => string 'Moscow' (length=6)

3 个答案:

答案 0 :(得分:0)

在控制器中删除此行操作:

$testForm->submit($request->request->get($testForm->getName()));

改变:

$testForm->handleRequest($request);

if ($testForm->isValid()) {

   $testFormData = $testForm->getData();
   exit(\Doctrine\Common\Util\Debug::dump($testFormData));

}

答案 1 :(得分:0)

似乎Debug :: dump是自己的printig数据。首先调用它,然后在没有任何参数的情况下退出

答案 2 :(得分:0)

将您的控制器更新为:

/**
 * @Route("/test/add", name="addTest", defaults={"_locale": "sr"}, requirements={
 *    "_locale": "en|sr"
 * })
 * @Method({"GET", "POST"})
 */
 public function addTestAction(Request $request) {

   $test = new Test();
   $testForm = $this->createForm(new TestType(), $test);

   $testForm->handleRequest($request);

   if ($testForm->isValid()) {

       $testFormData = $testForm->getData();
       exit(\Doctrine\Common\Util\Debug::dump($testFormData));
   }

   return $this->render(
       'AppBundle:trt:addTest.html.twig', array(
           'form' => $testForm->createView(),
       )
   );
}

之所以,你打电话提交两次:

  

$ testForm-&GT;提交($请求 - &GT;请求 - &GT;获得($ testForm-&GT;的getName()));

提交表单时调用,这就是为什么你有两次数据。