1

I'm building app in symfony2.4 and angularjs. In angular I've created resource:

app.factory('Tasks', ['$resource', function($resource) {
    return $resource( 'api/tasks/:taskid', null,
         {
            'update': { method:'PUT' }
        });
}]);

and when I'm trying to update like this: Tasks.update({ taskid : task.id },task);I'm getting this error:

{"code":400,"message":"Validation Failed","errors":{"errors":["The CSRF token is invalid. Please try to resubmit the form."],"children":{"timeStart":{"children":{"date":[],"time":[]}},"timeStop":{"children":{"date":[],"time":[]}},"project":[],"descriptionTask":[],"isCompleted":[],"isVisible":[]}}}

When I build form in symfony and then update any task - it works. But I noticed that AngularJS doesn't send any data via POST. It sends via JSON(? or just sends this object I'm giving ) and I think this is the problem. How can I repair it?

mmmm
  • 3,768
  • 9
  • 36
  • 63

1 Answers1

0

You need to add the _token in your form i.e

{{ form_row(form._token) }}

As of now your form is missing with the csrf token field,if you use the twig form functions to render your form like form(form) this will automatically renders the csrf token field for you but your code show your are rendering your form with raw html for form like <form></form> then you have to manually render the field

or simply add {{ form_rest(form) }} before closing tag of form

According to docs

This renders all fields that have not yet been rendered for the given form. It's a good idea to always have this somewhere inside your form as it'll render hidden fields for you and make any fields you forgot to render more obvious (since it'll render the field for you).

form_rest(view, variables)

Answer from @M Khalid Junaid in The CSRF token is invalid. Please try to resubmit the form

Community
  • 1
  • 1
chalasr
  • 12,971
  • 4
  • 40
  • 82