Disable CSRF protection in form generated by Sonata Admin

纵然是瞬间 提交于 2019-12-14 03:21:43

问题


In Sonata Admin I need to disable the CSRF token in some of my forms but sometimes I don't want to create a Form Type class, choosing instead to let Sonata generate the form, as such:

/** @var $form \Symfony\Component\Form\Form */
$form = $this->admin->getForm();

How can I disable the CSRF token from this point?


回答1:


Without a Form Type class, the best way to change the CSRF field would be in the admin Class. For that, it's possible to override this function:

public function getFormBuilder() {
    $this->formOptions['data_class'] = $this->getClass();
    $this->formOptions['csrf_protection'] = false;
    $formBuilder = $this->getFormContractor()->getFormBuilder(
        $this->getUniqid(),
        $this->formOptions
    );
    $this->defineFormBuilder($formBuilder);
    return $formBuilder;
}



回答2:


I think it's not possible as from \Symfony\Component\Form\Form instance you have access to object ($form->getConfig()) that implements FormConfigInterface: you can call on it getOptions or getOption but there is no setOption method.



来源:https://stackoverflow.com/questions/39143049/disable-csrf-protection-in-form-generated-by-sonata-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!