Search code examples
symfonysonata-admin

Disable CSRF protection in form generated by Sonata Admin


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?


Solution

  • 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;
    }