簡體   English   中英

Symfony2 CSRF形式,令牌無效

[英]Symfony2 csrf forms, token invalid

編輯:最后有一個Tl; Dr ...

使用symfony2和自動生成的表單時,我不斷收到CSRF錯誤。

這是我的控制器:(調用new來顯示表單,調用create時提交)

public function newAction($guru)
{

    //Make the Entity Manager
    $em = $this->getDoctrine()
            ->getEntityManager();
    $guru = $em->getRepository('TSNStatsBundle:Guru')
            ->findOneById($guru);
    //If the guru id exists        
    if ($guru)
    {
        $alert = new Alert();
        //Create default values
        $time = new \DateTime(2012-12-30);
        $time->setTime(23,59);

        //Set default times to "none available (23:59)"
        $alert->setText($time)
        ->setEmail($time)
        ->setTwitter($time)
        ->setChat($time)
        ->setGuru($guru);

        //Make the form, set types, 
        $formBuilder = $this->createFormBuilder($alert);


         $formBuilder->add('buy', 'checkbox', array(
                    'required' => false
                ))
                ->add('date', 'date', array(
                    'input' => 'datetime',
                    'widget' => 'single_text'
                ))
                ->add('stock', new StockType());
        if ($guru->getInstantAlerts() ==1)
        {
            if ($guru->getText() == 1)
            {
                $formBuilder->add('text', 'time', array(
                       'input' => 'datetime',
                       'widget' => 'text',

                   ));
            }
            if ($guru->getEmail() == 1)
            {
                $formBuilder->add('email', 'time', array(
                       'input' => 'datetime',
                       'widget' => 'text',

                   ));
            }
            if ($guru->getTwitter() == 1)
            {
                $formBuilder->add('twitter', 'time', array(
                       'input' => 'datetime',
                       'widget' => 'text',

                   ));
            }
            if ($guru->getChat() == 1)
            {
                $formBuilder->add('chat', 'time', array(
                       'input' => 'datetime',
                       'widget' => 'text',

                   ));
            }
        }
        $formBuilder->add('size')
                ->add('short', 'checkbox', array(
                    'required' => false
                ))
                ->add('his')
                ->add('guru');
         $form = $formBuilder->getForm();



        return $this->render('TSNStatsBundle:Buy:new.html.twig', array(
            'form' => $form->createView(),
            'guru' => $guru
        ));



    }
    else
    {
        //your guru ain't real bro!
    }
    return $this->render('TSNStatsBundle:Buy:new.html.twig', array(
        'alert' => $alert,
        'form' => $form->createView(),
        'guru' => $guru->getName()

     ));
}

public function createAction()
{
    $alert = new Alert();

    $form = $this->createForm(new AlertType(), $alert);
    $request = $this->getRequest();
    if ($this->getRequest()->getMethod() == 'POST') {
        $form ->bind($request);


        if ($form->isValid())
        {
            $em = $this->getDoctrine()
                    ->getEntityManager();
            $em->persist($alert);
            $em->flush();

            return $this->redirect($this->generateUrl('new_alert', array(
                'guru' => 2
            ) ));

        }
    }

    return $this->render('TSNStatsBundle:Buy:errors.html.twig', array(
          'errors' => $form->getErrors()
    ));

}

這是我的模板:

Adding entry for {{ guru }}
<form action="{{ path('create_alert' ) }}" method="post" {{ form_enctype(form) }} class="alert">
{{ form_widget(form) }}
<p>
    <input type="submit" value="Submit">
</p>
</form>

據我所知,一切都是由書決定的。 每次刷新時,_token值都會以每種形式出現,它會調用它的小部件,因此所有部分都應該存在。

謝謝,

編輯:當我將整個表單創建過程替換為:

$form = $this->createForm(new AlertType(), $alert);

然后它再次起作用。 問題是我想要的邏輯不屬於“類型”類。 那以及我做事的方式應該正確嗎? 它可以與我向表單中添加元素的方式有關嗎? 對於我的構建與createForm()構建,這是我唯一看到的不同之處。

Tl; Dr:使用帶有* entity * Type調用的createForm調用可以很好地工作,使用createFormBuilder()創建我自己的表單在每次提交時都會遇到CSRF錯誤。...相同的_token用於兩者。

也許使用這個可以幫助您:

{{form_widget(form._token)}}

嘗試替代

{{ form_widget(form) }}
{{ form_rest(form) }}

對於

{{ form_widget(form) }}

您可以將相同的$ options數組(如表單類型)傳遞給FormBuilder,然后可以通過以下方式關閉csrf保護:

$this->createFormBuilder($object, $options = array(
    'csrf_protection' => false,
));

原始示例: http : //symfony.com/doc/current/book/forms.html#csrf-protection

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM