Hi everyone I have this problem with my form
The CSRF token is invalid. Please try to resubmit the form
I show my form in a view like that
<form id="target" action="#" data-idea="{{idea}}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
</br>
</br>
<input type="submit" value="add comment"/>
</form>
In my controller I have this
public function addCommentAction(){
$ideaId= $_POST['idea'];
$coment= new Comentario();
$form= $this->createForm(new ComentarioFormType(), $coment);
$request= $this->getRequest();
if($request->getMethod() == 'POST'){
$form->bindRequest($request);
if ($form->isValid()){
$em= $this->getDoctrine()->getEntityManager();
$coment->setIdea($ideaId);
$em->persist($coment);
$em->flush();
}
...
}
I have the error in ($form->isValid()), If I debug the code, isValid function return false when ask this … if ($this->hasErrors()) and show the error.
If I errase the line if ($form->isValid()) in my controller , the funcion don´t persist the form because the field descripcion is null.
this is my form
public function buildForm( FormBuilder $builder, array $options)
{
$builder->add('descripcion', 'textarea');
}
public function getDefaultOptions( array $options){
return array('data_class'=> 'ComentBundle\Entity\Coment');
}
public function getName()
{
return 'coment';
}
Any idea?
Try this
note the $request is from Action declare statement
I hope this will help