2

I'm currently trying to combine a Symfony Form with angularJS... A Service posts data to a form, that should save an entity to the database. Unfortunately, ngResource sends the data as a JSON payload, so that Symfony Forms can't process it... I tried many things on angular side, like changing the headers:

headers : {'Content-Type': 'application/x-www-form-urlencoded'}

I couldn't find much more on angular side, so I thought that I could find a Solution on Symfony Side. Any Idea how I could get this to work? Angular-Solutions are welcome too of course.

1
  • I would suggest leaving the AngularJS side alone and stick with basic json. That is more or less standard. There are a number of articles showing how to use Symfony 2/REST/Forms. It will take a bit of effort to get everything going but after that it is smooth sailing. Try: welcometothebundle.com/symfony2-rest-api-the-best-2013-way as a start. Read the referenced 2012 article if the 2013 article is too terse. Commented Sep 10, 2014 at 14:53

1 Answer 1

1

I finally found a solution, after reading deeper in the documentation. Symfony\Component\Form\Form::bind doesn't require a Request, it works with an array too. so here's my Solution (the sloppy way, would need some checking of the header, etc. for production use..)

public function setFooAction(Request $request){

  $form = $this->createForm();//get the form class, etc...
  $json_data = json_decode($request->getContent(),true);//get the response data as array
  $form->bind($json_data); //Bind data to Form

  if ($form->isValid()) {
    ...

  }

}
Sign up to request clarification or add additional context in comments.

3 Comments

as of Symfony v2.3 $form->bind() is deprecated and will be removed in v3.0 - should use $form->submit() instead.
I get lots of "This form should not contain extra fields" errors doing this :\
if you simply send an entire angular "entity", you'll probably get a bunch of angular specific variables, which of course are not mapped in your form... check the playload for that, a quick and dirty trick to fix this is to convert it to json and back to an object with angular...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.