SYMFONY2 - CSRF令牌无效Allwais

时间:2015-08-03 10:54:27

标签: symfony csrf-protection

所以......基本上就是这样。我有一个表单类,然后我渲染但总是返回csrf错误。

表格类:

class FormTest extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
           ->add(
                'urlTo',
                'url'
           )
        ->add('submit', 'submit');
    }

public function getName()
{
    return '';
}

}

在控制器中:

...
$form = $this->createForm(new FormTest ());

$form->handleRequest($request);

    if ($form->isSubmitted()) {
        if ($form->isValid()) {
            echo 1; die;
        }
    }

当我在“isSubmited”中看到并尝试“getData”时,我只是获取了url字段,令牌不是,我不知道是否正确。

查看:

 {{ form_start(form) }}

                {{ form_widget(
                form.linkTo,
                {
                    'attr' : {
                    'class' : 'form-control col-xs-12',
                    'placeholder' : 'url here'
                }
                }
                ) }}

                {{ form_errors(form) }}

                {{ form_widget(
                form.submit,
                {
                    'label' : ' START',
                    'attr' : {
                    'class' : 'btn glyphicon glyphicon-send'
                }
                }
                ) }}

                {{ form_end(form) }}

就是这样,ALLWAYS返回csrf令牌无效。

好的,问题是令牌不是会话中的存储......但我不知道为什么

1 个答案:

答案 0 :(得分:0)

您需要使用form_rest(form)来渲染剩余字段,包括令牌,或者您需要使用form_row(form._token)手动呈现令牌。

form_end(form)基本上只是</form>

相关问题