问题
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