I have a symfony form. Submitting it, I have an error message :
The CSRF token is invalid. Please try to resubmit the form.
I dont know why I have this. I bind request to form after checking the request method is post :
if ($request->getMethod() === 'POST') {
$form->handleRequest($request);
}
Here is the type of the form.
class PasswordActionType extends AbstractType {
protected $forgotten_password;
public function __construct($forgotten_password) {
$this->forgotten_password = $forgotten_password;
}
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, array $options) {
$builder->add('identifiant', 'text', array('attr' => array('style' => 'width:250px')));
if(!$this->forgotten_password) {
$builder->add('ancienMDP', 'password', array(
'label' => 'Ancien MDP',
'attr' => array(
'class' => 'ligne',
'style' => 'width:252px'
)));
$builder->add('nouveauMDP', 'repeated', array(
'type' => 'password',
'invalid_message' => 'Confirmation différente du nouveau mot de passe',
'first_options' => array('label' => 'Nouveau MDP'),
'second_options' => array('label' => 'Confirmer'),
'options' => array(
'attr' => array(
'class' => 'ligne',
'style' => 'width:252px'
))
));
} else {
$builder->add('ancienMDP', 'hidden', array('error_bubbling' => false, 'data' => 'NULL'));
$builder->add('nouveauMDP', 'hidden', array('error_bubbling' => false, 'data' => 'NULL'));
}
}
public function configureOptions(\Symfony\Component\OptionsResolver\OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => 'My\Bundle\Security\PasswordAction',
'csrf_protection' => true,
'csrf_field_name' => 'token'
));
}
public function getName() {
return 'cramif_password_action';
}
}
So 2 forms use the same builder. The difference is the value of "forgotten_password".
form1 : forgotten_password = true
the 2 fields 'ancienMDP' and 'NouveauMDP' are hidden html fields
form2: forgotten_password = false
the 2 fields are what u can see.
There is not problem with form1, no CSRF error.
The problem occurs with form2.
Note : the 2 forms are displayed with Twig with the same commands.
Note2 : in the twig template I have a form_rest