simplethings/SimpleThingsFormExtraBundle — pull request #30 by beberlei (2012-07-13 23:21:50+00:00)
Title: Form Serializer Extension

This form extension allows to serialize object graphs from form types, and bind serialized data to forms:

To deserialize just use the form framework:

public function editAction(Request $request)
{
$user = $this->get('doctrine.orm.default_entity_manager')
->find('Acme\DemoBundle\Entity\User', $request->get('id'));
$form = $this->createForm(new UserType(), $user);

if ($request->getMethod() === 'POST') {
// detects+decodes Content-Type: application/json and text/xml
$form->bind($request);

if ($form->isValid()) {
// do stuff
}
}

return array('form' => $form->createView(), 'user' => $user);
}

To serialize:

public function viewAction(Request $request)
{
$user = $this->get('doctrine.orm.default_entity_manager')
->find('Acme\DemoBundle\Entity\User', $request->get('id'));

$serializer = $this->get('form_serializer');
$xml = $serializer->serialize($user, new UserType(), 'xml');

return new Response($xml, 200, array('Content-Type' => 'text/xml'));
}