When we are using formWidgets csrf_token required error occurs
There are certian reasons for that and have solutions for that
First we see when this error will occur
1. When you are using render function in your template
2. When you are adding an extra field
3. When you are changing the behaviour of the fields
4. When we are not binding the form
Solutions
1. When you are using render function in your template
use the below code in your template
<?php echo $form['_csrf_token']->renderLabel() ?>
<?php echo $form['_csrf_token']->render() ?>
afer that refresh the page and view the source code of the page it should have a hidden field called _csrf_token and the value should not be empty. Suppose if it is empty check the csrf token value in your setting.yml it should have some value.
After adding refresh and submit your page. Now it should not ask for _csrf_token required error. If it does then see for next possible solution
2. When you are adding an extra field
You might missed the validation for the field. Please add the validation
and check out this for adding a extra field.If the validations are correct and still the error occurs then see the next possible solution
3. When you are changing the behaviour of the fields
This is a rare condition which would occur. The situation is this ou have changed the bahaviour of the control/field in your template/form/base form class but validations may not match for the control you have modified. If so then _csrf_token error would occur. So please check it once again and check the next solution
4. When we are not binding the form
This would be the error we make most of the times. When we submit the form we should not directly save the form it should be binded and then it should be checked isvalid and then we have to save the form. Sample function to save a form could be like this
public function processForm($request sfWebRequest, $form sfForm,$redirect)
{
$form->bind(
$request->getRequestParameter($form->getName()),
$request->getRequestParameter($form->getFiles())
);
if($form->isValid())
{
$form->save();
$this->redirect($redirect);
}
}
Hope this helps someone.....
No comments:
Post a Comment