How change csrf token in Symfony2/3 when form was submitted, validated and saved form's data to server?
// AppController.php
...
public function saveAction(Request $request) {
$event = new Data();
$form = $this->createForm(DataForm::class, $event);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$creator = $this->getDoctrine()
->getRepository('AppBundle:User')
->find($this->getUser()->getId());
$event->setCreator($creator);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($event);
$entityManager->flush();
< SOMETHING HERE to change csrf token. ANY IDEAS? >
return $this->redirect($this->generateUrl('event_view', ['id' => $event->getId()]));
}
$message = ['text' => 'Wrong data to save', 'type' => 'danger'];
$this->get('session')->set('messages', [$message]);
return $this->redirect($this->generateUrl('event_create'));
}
...
It should be regenerating the CSRF token automatically. The only reason I could think that you may want to do this would be if you submit the form with ajax and for whatever reason needed to get the next token: In your form type:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
//...other defaults
'intention' => 'data_form_intention',
));
}
In your saveAction:
$csrf = $this->get('security.csrf.token_manager');
$token = $csrf->refreshToken("data_form_intention");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.